new \CURLFile($filePath)); $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); if ($output === FALSE){ # 增加报警 EmailQueue::rPush('图片全路径上传失败', curl_error($curl), ['xiaohua.hou@kuxuan-inc.com'], '图片全路径上传失败'); Log::logError('上传图片HTTP请求失败', [ 'error' => curl_error($curl), 'url' => $url, 'path' => $filePath, ], 'HttpUploadError'); } 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; } }