依赖 Guzzle 7.x
GET
$client = guzzle_http();
$res = $client->request('GET', $url);
return (string)$res->getBody();
POST
$res = $client->request('POST', $url,['body'=>$body]);
return (string)$res->getBody();
POST JSON
$res = $client->request('POST', '/json.php', [
'json' => ['foo' => 'bar']
]);
发送application/x-www-form-urlencoded POST请求需要你传入form_params
$res = $client->request('POST', $url, [
'form_params' => [
'field_name' => 'abc',
'other_field' => '123',
'nested_field' => [
'nested' => 'hello'
]
]
]);
PUT
$body = file_get_contents($local_file);
$request = new \GuzzleHttp\Psr7\Request('PUT', $upload_url, $headers=[], $body);
$response = $client->send($request, ['timeout' => 30]);
if($response->getStatusCode() == 200){
return true;
}
随机IP
$opt = guzzle_http_fake_option();
$opt['referer'] = '';
$opt['form_params'] = [
'kw' => $kw,
'page'=> $page,
];
guzzle_http()->request('POST', $url, $opt);
$res = (string)$res->getBody();