tags:

views:

76

answers:

3

Hello Everyone!

I cannot figure out why this code works locally on my PC (localhost) but not online on a public server? Can it be a PHP version issue? Thankful for all help!

$post_data = array('item_type_id' => '8', 'string_key' => 'Test Nyckel2', 'string_value' => 'Test Varde2', 'string_extra' => 'Test Extra', 'numeric_extra' => 'Test Numeric Extra', 'is_public' => true, 'is_public_for_contacts' => true);

    $post_data = json_encode(array('item' => $post_data), JSON_FORCE_OBJECT);

    $c = curl_init('http://example.com/items.json'); 

    curl_setopt($c, CURLOPT_VERBOSE, 1);
    curl_setopt($c, CURLOPT_COOKIE, 'fb_cookie='.$fb_code);
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($c, CURLOPT_POSTFIELDS, $post_data);

    curl_exec($c);

    curl_close($c);
A: 

Is the cURL Library installed on the server that isn't working?

If you can't tell, use the phpinfo(); function.

Francisc
Yes, it is. Other cURL calls works fine. The only difference is that the cURL calls that does not work is sending along a JSON object. Can this be the problem?
The working server got PHP verion 5.3 and the none working got 5.2.
How about an IP restriction on the server you are trying to get data from?
Francisc
+1  A: 

I see you are using the options parameter in your json_encode() call.

However here is what PHP doc says (http://www.php.net/manual/en/function.json-encode.php):

5.3.0 The options parameter was added.

So you PHP code is using an undefined constant, JSON_FORCE_OBJECT?

R. Hill
+1  A: 

curl_exec returns FALSE if the request fails for any reason. You can then get the error codes and message with curl_error() and curl_errno():

if (curl_exec($c) === FALSE) {
    die("Curl failed: " . curl_error($c));
}

Never assume that curl calls will succeed. Always check the returned value in case something did blow up. Even if curl's set up properly, a network glitch could've killed the connection, the remote server could be down, firewall's having a bad day, etc...

Marc B