PHP cURL适用于Fiddler但不适用于我的服务器

时间:2014-09-16 14:31:10

标签: php curl http-headers

所以我用fiddler以及Rest Console和Advanced Rest Console对它进行了测试,一切正常,但是当我从cURL运行时,我得到连接超时错误或无法连接到主机。网址是相同的,标题是相同的,内容正文是相同的。我只是不明白。 编辑:在服务器上没有授权,代码工作很精细

这里差不多有5个小时,这里是代码:

function getLicenses() {

    $license = new LicenseConnector();
    $curl    = curl_init();
    $headers = array(
        'Authorization: Bearer 12345...',
        'Content-Type:  application/x-www-form-urlencoded'
    );


    $myPost = 'RequestingAgency=Woo&AcctEmail=&LengthOfLic=365&Demo=on';
    curl_setopt_array( $curl, array(
        curl_setopt($curl, CURLOPT_URL, 'http://example.com/api/serialnumbers/getnewserial'),
        curl_setopt( $curl, CURLOPT_POST, 1 ),
        curl_setopt( $curl, CURLOPT_POSTFIELDS, $myPost ),
        curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 ),
        curl_setopt($curl, CURLOPT_HEADER, 1),
        curl_setopt($curl, CURLINFO_HEADER_OUT, true),
        curl_setopt( $curl, CURLOPT_HTTPHEADER, $headers )
    ) );

    $result  = curl_exec( $curl );
    $results = json_decode( $result );

    if ( curl_errno( $curl ) ) {
        echo 'error:' . curl_error( $curl );
    } else {

        echo $results;
    }
    curl_close( $curl );

}

然后我运行函数......

2 个答案:

答案 0 :(得分:0)

您确定已在服务器上启用并安装了cUrl吗?

这是一个操作方法: PHP CURL Enable Linux

答案 1 :(得分:0)

所以,我重写了我的代码,一切正常......不是正常吗?

$ch = curl_init();
    $headers = array(
        "Authorization: Bearer $auth",
    );
    curl_setopt($ch, CURLOPT_URL,"http://example.com/api/serialnumbers/getnewserial2");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_POSTFIELDS,
        "RequestingAgency=Woo&AcctEmail=&LengthOfLic=365&Demo=on");


    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $server_output = curl_exec ($ch);

    curl_close ($ch);

    echo $server_output;