douyiji3919 2010-07-19 15:57
浏览 95
已采纳

PHP cURL代码在线失败但在localhost上失败。 请帮忙!

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);
  • 写回答

3条回答 默认 最新

  • donglin8467 2010-07-19 17:27
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?