php guzzle 绑定 host 访问接口

1,435 阅读1分钟

平常开发的时候可能没有测试域名,只有公司的内网ip,pc上可以改host 文件来访问,比如

1.2.3.4 xxx.com.cn

然后直接访问 xxx.com.cn 这个接口,但代码里如何也能这样呢? 这就需要在header中添加host了 ,这里用的guzzle,curl也一样的。

$client = new Client();
$response = $client->get('http://1.2.3.4',["timeout"=>1,"connect_timeout"=>1,'headers'=>['host'=>'xxx.com.cn']]);
$status = $response->getStatusCode();
if ($status == 200)
{
    $data=json_decode((string)$response->getBody(), true);
}
return false;