PHP cURL code fails online but not on localhost. Please help!
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$c);
curl_close($c);
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...
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?
Is the cURL Library installed on the server that isn't working?
If you can't tell, use the phpinfo(); function.
精彩评论