PHP 中的 CURL 在从开发服务器运行时不连接


CURL in PHP does not connect when run from development server

我在PHP中使用cURL将视频上传到Wistia。在我的本地服务器中一切正常。但是在开发服务器中,视频无法上传。使用 var_dump(curl_getinfo($ch)),我可以看到content_type与本地服务器不同。我对此感到困惑。任何人都可以帮助我解决这个问题。

这是我的代码:

public function video_upload($filePath)
{         
    $data = array(
        'api_password'  => '0XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX5',
        'file'          => '@'.$filePath,
    );
    $url = 'https://upload.wistia.com';    
    $ch  = curl_init();
    curl_setopt_array($ch, array(
        CURLOPT_URL            => $url,
        CURLOPT_POST           => true,
        CURLOPT_POSTFIELDS     => $data,
        CURLOPT_HEADER         => false,
        CURLOPT_RETURNTRANSFER => true,
    ));
    curl_setopt($ch, CURLINFO_HEADER_OUT, true);
    $response = curl_exec($ch);
    var_dump(curl_getinfo($ch));
    var_dump(curl_errno($ch));
    curl_close($ch);
    return $response;
}

我的本地服务器收到的响应:

array(27) {
    ["url"]=> string(26) "https://upload.wistia.com/"
    **["content_type"]=> string(30) "application/json;charset=utf-8"**
    ["http_code"]=> int(200)
    ["header_size"]=> int(688)
    ["request_size"]=> int(189)
    ["filetime"]=> int(-1)
    ["ssl_verify_result"]=> int(0)
    ["redirect_count"]=> int(0)
    ["total_time"]=> float(17.850026)
    ["namelookup_time"]=> float(0.252903)
    ["connect_time"]=> float(0.253271)
    ["pretransfer_time"]=> float(1.903306)
    ["size_upload"]=> float(279250)
    ["size_download"]=> float(417)
    ["speed_download"]=> float(23)
    ["speed_upload"]=> float(15644)
    ["download_content_length"]=> float(417)
    ["upload_content_length"]=> float(279250)
    ["starttransfer_time"]=> float(2.173591)
    ["redirect_time"]=> float(0)
    ["redirect_url"]=> string(0) ""
    ["primary_ip"]=> string(13) "162.209.95.19"
    ["certinfo"]=> array(0) { }
    ["primary_port"]=> int(443)
    ["local_ip"]=> string(13) "192.168.1.157"
    ["local_port"]=> int(54999)
    ["request_header"]=> string(189) "POST / HTTP/1.1 Host: upload.wistia.com Accept: */* Content-Length: 279250 Expect: 100-continue Content-Type: multipart/form-data; boundary=------------------------370a5719d6336ecc "
} int(0)

在我的开发服务器中收到的响应:

array(27) {
    ["url"]=> string(26) "https://upload.wistia.com/"
    **["content_type"]=> string(23) "text/html;charset=utf-8"**
    ["http_code"]=> int(500)
    ["header_size"]=> int(718)
    ["request_size"]=> int(186)
    ["filetime"]=> int(-1)
    ["ssl_verify_result"]=> int(0)
    ["redirect_count"]=> int(0)
    ["total_time"]=> float(0.437061)
    ["namelookup_time"]=> float(0.004766)
    ["connect_time"]=> float(0.023656)
    ["pretransfer_time"]=> float(0.194844)
    ["size_upload"]=> float(319)
    ["size_download"]=> float(30)
    ["speed_download"]=> float(68)
    ["speed_upload"]=> float(729)
    ["download_content_length"]=> float(30)
    ["upload_content_length"]=> float(319)
    ["starttransfer_time"]=> float(0.216544)
    ["redirect_time"]=> float(0)
    ["redirect_url"]=> string(0) ""
    ["primary_ip"]=> string(15) "162.242.168.223"
    ["certinfo"]=> array(0) { }
    ["primary_port"]=> int(443)
    ["local_ip"]=> string(14) "224.178.240.48"
    ["local_port"]=> int(55164)
    ["request_header"]=> string(186) "POST / HTTP/1.1 Host: upload.wistia.com Accept: */* Content-Length: 319 Expect: 100-continue Content-Type: multipart/form-data; boundary=----------------------------d45c07c28860 "
} int(0) 

很难说为什么它不起作用,但是您从服务器获得的响应包括一个500的http状态代码,这表明服务器上出了问题。

您这边可能有问题,但是如果没有来自服务器的更多信息,就很难判断出了什么问题。通常,来自服务器的500响应指示服务器 (wistia) 错误,而不是客户端 (u) 错误。

您可能希望将信息发送给 wistia 以获取更多详细信息。

您在第二个响应中收到来自服务器的 500 错误。这就是为什么它不是 json。

我敢打赌你正在做一个完全不同的 POST 请求,喜欢

$verbosefileh=tmpfile();
$verbosefile=stream_get_meta_data($verbosefileh)['uri'];
curl_setopt_array($ch,array(
CURLOPT_CERTINFO=>true,
CURLOPT_VERBOSE=>true,
CURLOPT_STDERR=>$verbosefileh
));
curl_exec($ch);
$postrequest=file_get_contents($verbosefile);

然后仔细研究 POST 请求,2 台服务器发送的请求有什么区别? 我敢打赌有一些东西..除非你被阻止在IP禁令之外

编辑:一个常见的问题,在某些安装中,curl 具有默认的用户代理,而在某些安装中,curl 没有。(就像在Debian 6中一样,它类似于"curl/7.21.3(x86_64-unknown-linux-gnu)libcurl/7.21.3 OpenSSL/1.0.0c zlib/1.2.5",而在Debian 8中,没有默认字符串......还是相反?),许多网站会阻止不包含useragent的请求。为了确保您有一个用户代理,您可以使用curl_setopt($ch,CURLOPT_USERAGENT,'curl php');