2016-11-21 14 views
1

私の考えは、Notificationsというモデルを使用して、データベースに保存されているすべてのURLにhttps要求を送信することです。何らかの理由でlaravel:httpリクエストでエラーが発生しました

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

getメソッドは文字列を期待し、それは私にエラーを与えた:

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

が、私はこれをどのように修正することができますか?誰でも良いアイデアがありますか?
これは私の通知クラス、実際にあなたがちょうどあなたがClientクラスを使用しているが、あなたが私たちのために必要とされるすべてのコードを示していないので、use文の痕跡がここにありません言っている

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(); 
    } 

答えて

0

ですこれを把握してください。 getメソッドのパラメータが何であるか分かりません。私の推測では、この文からNotificationのクラスエンティティの配列が返ってきています:$notes=Notification::all();

まず最初に、それらを反復処理してから、それぞれのクライアントに対してクライアントを呼び出す必要があります。しかしまた、getメソッドに文字列を指定する必要があるかもしれません。 Notificationクラスについてもコードがないので、どうやって言うことができません。

EDIT:

あなたは私はあなたがこのような何かを試みるべきだと思い提供されたコードを考える:エラーメッセージが言うように

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

     foreach ($notes as $note) { 
      $response = $client->get($note->website_url); 
      $response = $response->getStatusCode(); 
      var_dump($response); 
     } 
    } 
} 
+0

名前空間App; App \ Statusを使用します。 App \ Notificationを使用します。 Illuminate \ Database \ Eloquent \ Modelを使用します。 クラス通知モデル 延び{ \t保護$の充填可能な= [ 'ID'、 'WEBSITE_URL'、 'メール'、 'slack_channel'、 'check_frequency'、 'alert_frequency'、 'speed_frequency'、 'アクティブ']。 \t public function status(){ \t \t return $ this-> belongsToMany( 'App \ Status') - > withPivot( 'values') - > withTimestamps(); \t} –

+0

コメントを投稿するのではなく、質問を更新する必要があります。 –

+0

アップロードしました!新しいコードはこのようになります –

0

を、がつがつ食うクライアントのget()方法は、文字列またはどちらかを受け入れ実装はUriInterfaceです。 Notificationモデル(これはURIの配列ではなくIlluminate\Support\Collectionを返します)からデータを取得し、それをクライアントに直接送ります。クライアントのためにデータを準備する必要があります。このようなもの:

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); 
     } 
    } 
} 
+0

それは理にかなっています。 get($ note-> url)の代わりに、get($ note-> website_url)にするべきではありません。 URLの名前がweb_url列の通知テーブルに保存されるためですか? –

+0

ええ、確かに。私はそれが 'website_url'であることを知らなかった。 – sepehr

+0

tnxは完璧に動作します!質問は –

関連する問題