Php laravel:http请求给出错误
我的想法是使用名为Php laravel:http请求给出错误,php,laravel,Php,Laravel,我的想法是使用名为通知的模型向保存在数据库中的所有URL发送https请求 类控制器扩展控制器 { 公共职能部门{ $client=新客户端(); $notes=Notification::all(); $response=$client->get($notes); $response=$response->getStatusCode(); var_dump($response); } } 出于某种原因,get方法需要字符串,但它给了我一个错误: functions.php第62行中的Inva
通知
的模型向保存在数据库中的所有URL发送https请求
类控制器扩展控制器
{
公共职能部门{
$client=新客户端();
$notes=Notification::all();
$response=$client->get($notes);
$response=$response->getStatusCode();
var_dump($response);
}
}
出于某种原因,get方法需要字符串,但它给了我一个错误:
functions.php第62行中的InvalidArgumentException:URI必须是字符串或URI接口
我怎样才能解决这个问题?有谁有更好的主意吗?这实际上是我的通知类
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();
}
你只是说你正在使用一个
客户端
类,但是这里没有use语句的痕迹,因为你没有显示我们解决这个问题所需要的所有代码。我们甚至不知道什么是get
方法参数。我猜您从这条语句中得到了一个通知类实体数组:$notes=Notification::all()代码>
因此,首先,您应该对它们进行迭代,然后对它们中的每一个调用客户机。但是您可能还需要为get
方法提供一个字符串。由于通知
类也没有代码,所以无法说明如何处理
编辑:
鉴于您提供的代码,我认为您应该尝试以下内容:
类控制器扩展控制器
{
公共职能部门
{
$client=新客户端();
$notes=Notification::all();
foreach($notes作为$note){
$response=$client->get($note->website\u url);
$response=$response->getStatusCode();
var_dump($response);
}
}
}
您只是说您正在使用一个客户端
类,但这里没有use语句的痕迹,因为您没有显示我们解决这个问题所需的所有代码。我们甚至不知道什么是get
方法参数。我猜您从这条语句中得到了一个通知类实体数组:$notes=Notification::all()代码>
因此,首先,您应该对它们进行迭代,然后对它们中的每一个调用客户机。但是您可能还需要为get
方法提供一个字符串。由于通知
类也没有代码,所以无法说明如何处理
编辑:
鉴于您提供的代码,我认为您应该尝试以下内容:
类控制器扩展控制器
{
公共职能部门
{
$client=新客户端();
$notes=Notification::all();
foreach($notes作为$note){
$response=$client->get($note->website\u url);
$response=$response->getStatusCode();
var_dump($response);
}
}
}
正如错误消息所说,guzzle客户端的get()
方法接受字符串或UriInterface
实现。您正在从通知
模型(该模型返回illumb\Support\Collection
而不是URI数组)获取数据,并将其直接提供给客户端。您需要为客户准备数据。大概是这样的:
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);
}
}
}
正如错误消息所说,guzzle客户端的get()
方法接受字符串或UriInterface
实现。您正在从通知
模型(该模型返回illumb\Support\Collection
而不是URI数组)获取数据,并将其直接提供给客户端。您需要为客户准备数据。大概是这样的:
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);
}
}
}
名称空间应用程序;使用App\Status;使用App\通知;使用Illumb\Database\Elount\Model;类通知扩展了模型{protected$filleble=['id','website\u url','email','slack\u channel','check\u frequency','alert\u frequency','speed\u frequency','active'];公共函数状态(){return$this->belongsToMany('App\Status')->with pivot('values')->with timestaps();}您应该更新您的问题,而不是将代码作为注释发布在此处。名称空间应用程序;使用App\Status;使用App\Notification;使用illumb\Database\elount\Model;类通知扩展模型{protected$filleble=['id'、'website\u url'、'email'、'slack\u channel'、'check\u frequency'、'alert\u frequency'、'speed\u frequency'、'active'];公共函数状态(){return$this->belongsToMany('App\Status')->withPivot('values')->withTimestamps()}您应该更新您的问题,而不是将代码作为注释发布在此处。这很有意义。它不应该是get($note->url),而应该是get($note->website\u url)。因为url的名称保存在“网站url”列的“通知”表中?是的,当然。我不知道它是网站url
。但有一个问题。它不是发送所有url的https请求,而是只发送第一个url。请更新您的问题,而不是答案。这可能是因为您正在覆盖e> foreach循环中的$response
。你怎么知道它只发送第一张便笺的请求?对不起!我编写的代码中有一个错误。我调试了它,它工作得很好!tnxit很有意义。它不应该是get($note->url),而应该是get($note->website\u url)。因为url的名称保存在“网站url”列的“通知”表中?是的,当然。我不知道它是网站url
。但有一个问题。它不是发送所有url的https请求,而是只发送第一个url。请更新您的问题,而不是答案。这可能是因为您正在覆盖e> $response
在foreach循环中。您怎么知道它只发送第一张便笺的请求?抱歉!我编写的代码中有一个错误。我调试了它,然后