new \CURLFile($filePathUpload)); $curl = curl_init(); curl_setopt($curl , CURLOPT_URL , $url); curl_setopt($curl , CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查 curl_setopt($curl , CURLOPT_POST, 1); curl_setopt($curl , CURLOPT_POSTFIELDS, $file_data); $output = curl_exec($curl); $responseData = json_decode($output, true); if ($output === FALSE || !isset($responseData['errcode'])){ Log::logError('上传图片HTTP请求失败', [ 'error' => curl_error($curl), 'url' => $url, 'upload_path' => $filePathUpload, 'path' => $filePath, 'retry' => $retry ], 'HttpUploadError'); if($retry<=5) { // 发起重试 sleep(1); $retry++; return self::httpUpload($url, $filePath, $retry); } else { // 报警 EmailQueue::rPush('图片全路径上传失败', curl_error($curl), ['xiaohua.hou@kuxuan-inc.com'], '图片全路径上传失败'); } } curl_close($curl); return $output; } /* * 获取响应头-不返回响应体 * */ public static function getRedirectUrl($url) { $redirect_url = false; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, TRUE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_SSLVERSION, false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_NOBODY, TRUE);//不返回请求体内容 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);//允许请求的链接跳转 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $content = curl_exec($ch); if($content === false){ echo 'Curl error: ' . curl_error($ch); } if(!curl_errno($ch)) { $redirect_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);//获取最终请求的url地址 } return $redirect_url; } /** * @param $url * @param array $body * @param array $headers * @param bool $jsonType * @param int $timeout 设置等待时间。秒,注意,若超过了等待时间,依然没有执行完毕,curl_exec() 将会返回false * @param bool $autoRedirect * @param bool $showResponseHeader * @return mixed */ public static function curl_post($url, $body = [], array $headers = [], $jsonType = true, $timeout = 30) { $opt = [ CURLOPT_URL => $url, //设置获取的信息以文件流的形式返回,而不是直接输出。 CURLOPT_RETURNTRANSFER => true, //要求结果为字符串且输出到屏幕上 //https请求 不验证证书和hosts CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_POST => true, CURLOPT_HEADER => false, // 不输出响应头信息 CURLOPT_HTTPHEADER => $headers, CURLOPT_POSTFIELDS => http_build_query($body), CURLOPT_TIMEOUT => $timeout, ]; if ($jsonType) { $headers[] = 'Content-Type: application/json'; $opt[CURLOPT_HTTPHEADER] = $headers; $opt[CURLOPT_POSTFIELDS] = json_encode($body); } $curl = curl_init(); // 初始化 curl_setopt_array($curl, $opt); $output = curl_exec($curl); // 执行 curl_close($curl); // 关闭cURL return $output; } /* * 封装HTTP请求 -- POST * */ public static function apiPost($url, $data = null,$json = FALSE, $headers = null){ //创建了一个curl会话资源,成功返回一个句柄; $curl = curl_init(); //设置url curl_setopt($curl, CURLOPT_URL, $url); //设置为FALSE 禁止 cURL 验证对等证书(peer’s certificate) curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); //设置为 1 是检查服务器SSL证书中是否存在一个公用名(common name) curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($curl, CURLOPT_HEADER, TRUE); //表示需要response header curl_setopt($curl, CURLOPT_NOBODY, FALSE); //表示需要response body if (!empty($data)){ //设置请求为POST curl_setopt($curl, CURLOPT_POST, 1); //curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60); //最长的可忍受的连接时间 //设置POST的数据域 curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } if($json){ $header = array( 'Content-Type: application/json; charset=utf-8', // 'Content-Length: ' . strlen($data) ); if(!empty($headers)) { $header = array_merge($header, $headers); } curl_setopt($curl, CURLOPT_HTTPHEADER, $header); } //设置是否将响应结果存入变量,1是存入,0是直接输出 curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); //然后将响应结果存入变量 $output = curl_exec($curl); $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); //关闭这个curl会话资源 curl_close($curl); $header = substr($output, 0, $headerSize); $body = substr($output, $headerSize); return [$header, $body]; } }