mardi 31 octobre 2017

Guzzle Error - cURL error 6: Couldn't resolve host

Description

If I have this in my class

class CURL {

public static function get($url) {

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_TIMEOUT_MS, 1000);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //<------ Note HERE 
    curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
    $result = curl_exec($ch);
    curl_close($ch);
    $result =  json_decode($result, true);
    return $result;

}

My page load fine.

enter image description here


If I have this in my class

<?php
namespace App;

use Request,Auth;
use App\Log, App\Helper;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client;



class CURL {

    public static function get($url) {

        $client = new Client();
        $result = $client->request('GET',$url, ['http_errors' => false, 'verify' => false ]);
        $result = (string) $result->getBody();
        $result = json_decode($result, true);
        return $result;

    }
    ...

}

As you can see, I tried set 'verify' => false already.

My page show this error.

enter image description here


Questions

How would one go about and debug this further ?


I'm open to any suggestions at this moment.

Any hints/suggestions / helps on this be will be much appreciated!



via Chebli Mohamed

Laravel 5 if statement between Carbon diffForHumans and toFormattedDatesString

I want to write an if statement to show timestamp in diffForHumans format for timestamps less than less than 5 days old and in toFormattedDatesString for anything older than that.

For example today is 31/10/17, so timestamp for 28/10/2017 should show - 3 days ago and timestamp for 20/10/2017 should show Oct 20, 2017.

Can someone please tell any logic to achieve this? Thanks in advance for the help.



via Chebli Mohamed

dimanche 29 octobre 2017

How to get full url of image after amazon s3 upload

I need to get full url of a image which is uploaded to amazon s3. I tried following functions. I am using laravel 5.1.

Storage::disk("s3")->url($filename);
Storage::url($filename);
Storage::temporaryUrl('file1.jpg', Carbon::now()->addMinutes(5));
$filesystem->getAdapter()->getClient()->getObjectUrl($bucket, $key);

all are showing undefined function url (or) temporaryUrl (or) getObjectUrl.



via Chebli Mohamed

samedi 28 octobre 2017

How to place a range in the model::list method for combo in laravel?

I would like you to help me, to solve my question, and I would like to know what is the way to range my combo of charges, which comes from the database in the description field, but It list all the records , and I just need to show some of them, so I need to know how to set a range for the mentioned combo, This is where my combo is that gives me all the charges that are registered in the database, to pass them to a View to a select tag:

//VIEW TO REGISTER USERS
  public function VistaRegistraUsuario()
{

  $id_franquicia_usuario = Session::get('id_franquicia');
  $charges = charges_users::lists('descripcion', 'id'); //COMBO OF CHARGES

  return view('auth.register', compact('charges', 'id_franquicia_usuario'));
}// END VIEW



via Chebli Mohamed

laravel 5.1 check if a session is expired

I'm working on a laravel 5.1 application and I want to check if a user session has expired after each request, in order to redirect the user to the login page. in order to do so I have created a middleware that runs on every request, the handle function looks like this

public function handle($request, Closure $next)
{
     if(session_status() === PHP_SESSION_NONE)
     {
          return redirect()->guest('login');
     }
     return $next($request);
}

this does not seem to work correctly, because when I type 'localhost:8000' in google chrome it says 'localhost redirected you too many times', I guess it is because the session have not been started since the user is not logged in, so... is there any better way to do this checking?



via Chebli Mohamed

vendredi 27 octobre 2017

Laravel 5.1: migrate command doesn't work 'unknown database'

Good day!

I am facing problems with my laravel project built with laravel 5.1.

So I developed a reservation system with laravel, it works on my local server.

Then I uploaded it to my live server and started an SSH session.

But when I ran php artisan migrate command, this error showed up

[PDOException] SQLSTATE[HY000] [2005] Unknown MySQL server host 'sddb0040291787.cgidb' (0)

So, what I did so far is I wrote a php script to create the tables and other database related stuff and then run the php script on my server.

This works actually, my CRUD functions are working but I know this is just a temporary solution.

What I am looking for is a permanent solution. Plain PHP scripts seem to be able to connect to the database host. but when I migrate using laravel, the error always shows up. Any ideas?



via Chebli Mohamed

jeudi 26 octobre 2017

Rewriting laravel 5.1 controller action route on the Request object

I have many modules my source code organized into subfolders inside the App\Http\Controllers e.g App\Http\Controllers\ModuleOne.

The base controllers are in App\Http\Controllers and the module controllers extend these base controllers. A module controller might not exist if I don't want to customize the base controller when using that particular module.

I want to write a logic where a route checks if the module controller exists. If the route does not exist, it should route the action to a BaseController.

I have tried to make a middleware and other solutions but can't seem to get this done.

I want to have the routing inside all the controllers done with the same name (thus ignoring the module name - which will be defined by an env variable). So, to simplify code I want to call:

Route::get('apple','AppleController@view')

and from this route it should check if:

App\Http\Controller\module1\module1_AppleController.php

exists.

If it does, use it. If not, it should route to the base controller action i.e. App\Http\Controller\AppleController.

Can't seem to figure out where to do this with efficient code. Can the rewrite be done in RouteServiceProvider in middleware or other?

Also, if the newer version of Laravel could present a solution not found in 5.1, I am willing to upgrade, so don't limit answers to 5.1.



via Chebli Mohamed