abraham foto abraham foto - 17 days ago 5
PHP Question

laravel: http request gave error

My idea is to send https request to all the URLs saved in my database using a model called

Notifications
.

class guzzleController extends Controller
{
public function guzzle() {
$client = new Client();
$notes=Notification::all();
$response = $client->get($notes);
$response=$response->getStatusCode();
var_dump($response);
}
}


For some reason the get method expects string, and it gave me an error:


InvalidArgumentException in functions.php line 62: URI must be a string or UriInterface


How can I fix this? Anyone with a better idea?

this is actually my notification class

namespace App;
use App\Status;
use App\Notification;

use Illuminate\Database\Eloquent\Model;

class Notification extends Model
{
protected $fillable = ['id','website_url','email','slack_channel','check_frequency','alert_frequency','speed_frequency','active'];

public function statuses(){
return $this->belongsToMany('App\Status')->withPivot('values')->withTimestamps();
}

Answer

As the error message says, the guzzle client's get() method accepts either a string or a UriInterface implementation. You're fetching the data from Notification model (which returns a Illuminate\Support\Collection not an array of URIs) and feed it directly to the client. You need to prep your data for the client. Something like this:

use Notification;
use GuzzleHttp\Client;

class GuzzleController extends Controller
{
    public function guzzle() 
    {
        $client = new Client();
        $notes  = Notification::all();

        // To do this in a more Laravelish manner, see:
        // https://laravel.com/docs/5.3/collections#method-each
        foreach ($notes as $note) {
            // Assuming that each $note has a `website_url` property  
            // containing the URL you want to fetch.
            $response = $client->get($note->website_url);

            // Do whatever you want with the $response for this specific note
            var_dump($response);
        }
    }
}