首页 > 解决方案 > laravel guzzle 无法正常工作页面卡在重新加载

问题描述

public function getGuzzleRequest()
{
    $client = new \GuzzleHttp\Client();
    $request = $client->get('http://localhost:8000/api/v1/poster');
    $response = $request->getBody();
    print_r($response);
}

laravel guzzle 无法正常工作页面卡在重新加载

标签: phplaravelguzzle

解决方案


PHP 开发服务器 (artisan: serve) 中的构建是单线程的,因此会挂起,例如http://localhost:8000将不起作用。尝试使用真实服务器(Apache /nginx)。像' http://ip.jsontest.com '

public function getGuzzleRequest(){
$client = new Client();
$request = $client->get('http://youripadress:8000/api/v1/poster');
$response = $request->getBody()->getContents();
print_r($response);

}

您也可以使用 php curl 功能提供替代方案。

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL,$targeturl);
            curl_setopt($ch, CURLOPT_POST,1);
            curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
            curl_exec ($ch);
            curl_close ($ch);

但请确保您使用的是真实服务器,如果可能的话,用您的 IP 地址补充 localhost

如何在命令行或终端上获取您的 ip 地址 运行ifconfig命令并检查前面的数字inet

你可以通过 php 了解更多关于guzzle 文档curl 请求的内容


推荐阅读