使用Guzzle 重写 使用curl的方法
Enviroment
php 7.3.3
laravel: 5.7
后台: laravel admin
guzzlehttp/guzzle: 6.5.8
My question:
使用Guzzle 重写 使用curl的方法。
发请求的时候,使用curl方法(request_socketpush_record)可以,但是我重写的函数(sendMessageOrigin)发不了这个请求,一直停在那里。
原因应该在curl_setopt($curl, CURLOPT_TIMEOUT, 5); 上面,但是我Guzzlehttp也加了。
public static function request_socketpush_record(array $data = [], string $type = 'common', $is_decode = true, $data_encode = 1)
{
try {
$url = config('app.host_ip') . ':' . (config('app.socket_port') - 1);
$curl = curl_init();
$this_header = [
"content-type: application/x-www-form-urlencoded;charset=UTF-8;Accept’:'Application/json",
];
curl_setopt($curl, CURLOPT_HTTPHEADER, $this_header);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
if (!empty($data)) {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 5); //请求时间 单位秒
$output = curl_exec($curl);
curl_close($curl);
RequestRecord::create([
'type' => $type,
'request' => json_encode($data),
'user_id' => 0,
'response' => json_encode($output, true),
]);
if ($is_decode) {
return json_decode($output, true);
}
} catch(Exception $e) {
RequestRecord::create([
'type' => $type,
'request' => json_encode($data),
'user_id' => 0,
'response' => json_encode([$e->getMessage(),$e->getTrace()]),
]);
}
}
public static function sendMessageOrigin(array $data, string $type = 'common')
{
$url = config('app.host_ip') . ':' . (config('app.socket_port') - 1);
$client = new Client();
try {
$res = $client->request('POST', $url, [
'headers' => [
'Accept'=> 'Application/json',
],
'form_params' => $data,
'connect_timeout' => 5
]);
RequestRecord::create([
'type' => $type,
'request' => json_encode($data),
'user_id' => 0,
'response' => $res->getBody(),
]);
return $res;
} catch(Exception $e) {
RequestRecord::create([
'type' => $type,
'request' => json_encode($data),
'user_id' => 0,
'response' => json_encode([$e->getMessage(),$e->getTrace()]),
]);
}
}
找到原因了,当初看参数的时候,看错了,附下源码Guzzlehttp的源码
if (isset($options['timeout'])) {
$timeoutRequiresNoSignal |= $options['timeout'] < 1;
$conf[CURLOPT_TIMEOUT_MS] = $options['timeout'] * 1000;
}
// CURL default value is CURL_IPRESOLVE_WHATEVER
if (isset($options['force_ip_resolve'])) {
if ('v4' === $options['force_ip_resolve']) {
$conf[CURLOPT_IPRESOLVE] = CURL_IPRESOLVE_V4;
} elseif ('v6' === $options['force_ip_resolve']) {
$conf[CURLOPT_IPRESOLVE] = CURL_IPRESOLVE_V6;
}
}
if (isset($options['connect_timeout'])) {
$timeoutRequiresNoSignal |= $options['connect_timeout'] < 1;
$conf[CURLOPT_CONNECTTIMEOUT_MS] = $options['connect_timeout'] * 1000;
}
我不知道我为啥用connect_timeout, 应该用timeout,我当初找的是CURLOPT_TIMEOUT 对应的参数,以为这个参数就是connect_timeout
没设 application/x-www-form-urlencoded的原因是官网写了,Use form_params for application/x-www-form-urlencoded requests, and multipart for multipart/form-data requests
Accept’:'Application/json 这个问题更大吧,你看下你curl里是怎么配置的
试试
curl设置了application/x-www-form-urlencoded,Guzzle没设
直接用https://github.com/Yurunsoft/YurunHttp好了