0

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. 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:

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...

Shane
  • 548
  • 1
  • 6
  • 17
  • And what does it output? – Jason McCreary Sep 16 '14 at 14:37
  • By the way, if I turn Authorization off on the server, the code works just fine. – Shane Sep 16 '14 at 14:39
  • This is the output: "error:couldn't connect to host" – Shane Sep 16 '14 at 14:40
  • Done any basic debugging, like trying to do a telnet to port 80 from the same machine's command line? "timed out" means your packets are getting lost somewhere. e.g. a firewall on either end. – Marc B Sep 16 '14 at 14:59
  • No, I haven't tried that, but the same code works without Authorization on... also, this is being ran from a shared host so I do not have shell access, on the server we are trying to hit we have allow-origin: * – Shane Sep 16 '14 at 15:03

2 Answers2

0

Are you sure that cUrl is enabled and installed on your server?

Here's a how-to: PHP CURL Enable Linux

Community
  • 1
  • 1
nico
  • 123
  • 1
  • 7
0

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;
Shane
  • 548
  • 1
  • 6
  • 17