douchuanghan1344 2015-12-05 19:15
浏览 82

远程服务器上的json响应无效

I have a very strange problem which I have been trying to figure out for 2 days, I have the below code to fetch information from a site for flight information, it works perfectly on my localhost and returns me "{thedata:what_I_Need}" but when I upload the script on my server it returns an "{invalid:true}" error from the site.

I tried it on 4 different droplets/vps, each in a different country IP with the same configuration (same curl-php-apache version and modules) as my local computer ( using ubuntu 14.4 with php5) but it still has the same issue. Here is the code which works perfectly on localhost:

   $url = 'http://site.url.com';
    $post = array(
        'LanguageID' => 2,
        'Trips' => array(array(
                    'Origin' => "JFK",
                    'Destination' => "TEX",
                    'Departure' => "2015-12-24T00:00:00",
                    )),
        'Return' => "2015-12-28T00:00:00",
        'Carriers' => null
        'Adults' => 1,
        'Children' => 0
    );  
    $data_string = json_encode($post); 

    $ch = curl_init($url);                                
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);
    curl_setopt($ch,CURLOPT_HEADER, 1); 
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
    curl_setopt($ch, CURLOPT_REFERER, "http://www.site.url.com");
    curl_setopt($ch, CURLOPT_TIMEOUT, 20);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
        'Content-Type: application/json',       
        'X-Requested-With: XMLHttpRequest',                                                                                
        'Content-Length: ' . strlen($data_string))                                                                       
    );
    curl_setopt($ch, CURLOPT_COOKIE, $cookie);
    $output=curl_exec($ch);
    curl_close($ch);

I followed this article as well but I still have same issue on the remote site even if I replace CURLOPT_POST with

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);    
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 腾讯企业邮箱邮件可以恢复么
    • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
    • ¥15 错误 LNK2001 无法解析的外部符号
    • ¥50 安装pyaudiokits失败
    • ¥15 计组这些题应该咋做呀
    • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
    • ¥15 让node服务器有自动加载文件的功能
    • ¥15 jmeter脚本回放有的是对的有的是错的
    • ¥15 r语言蛋白组学相关问题
    • ¥15 Python时间序列如何拟合疏系数模型