$token, 'conversionTypes' => $conversionTypes); $reqData = json_encode($reqData); // 发送完整的请求数据 // do some log //print_r('req data: ' . $reqData . "\n"); // 向百度发送数据 $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_URL, self::BAIDU_OCPC_URL); curl_setopt($ch, CURLOPT_POSTFIELDS, $reqData); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json; charset=utf-8', 'Content-Length: ' . strlen($reqData) ) ); // 添加重试,重试次数为3 for ($i = 0; $i < self::RETRY_TIMES; $i++) { $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($httpCode === 200) { // 打印返回结果 // do some log //print_r('retry times: ' . $i . ' res: ' . $response . "\n"); $res = json_decode($response, true); // status为4,代表服务端异常,可添加重试 $status = $res['header']['status']; if ($status !== 4) { curl_close($ch); return $status === 0; } } } curl_close($ch); return false; } }