简体   繁体   English

PHP cURL在Fiddler上有效,但在我的服务器上不可用

[英]PHP cURL Works on Fiddler but not on my server

So I have tested this with fiddler as well as Rest Console and Advanced Rest Console everything works just fine there, but when I run in from cURL I get either a connection timed out error or can't connect to host. 因此,我已经用fiddler以及Rest Console和Advanced Rest Console进行了测试,但是在那里一切正常,但是当我从cURL运行时,出现连接超时错误或无法连接到主机。 Url's are the same, headers are the same, content body is the same. 网址相同,标头相同,内容主体相同。 I just don't get it. 我就是不明白。 EDIT: WITHOUT AUTHORIZATION TURNED ON ON THE SERVER THE CODE WORKS JUST FINE 编辑:在服务器上未启用授权的情况下,代码可以正常工作

Almost 5 hours stuck here, here's the code: 这里停留了将近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 );

}

Then I run the function... 然后我运行该功能...

Are you sure that cUrl is enabled and installed on your server? 您确定已在服务器上启用并安装了cUrl吗?

Here's a how-to: PHP CURL Enable Linux 以下是操作方法: PHP CURL启用Linux

So, I rewrote my code and everything works... isn't that about normal? 所以,我重写了代码,一切正常……不是正常吗?

$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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM