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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何用stata画出文献中常见的安慰剂检验图
    • ¥15 c语言链表结构体数据插入
    • ¥40 使用MATLAB解答线性代数问题
    • ¥15 COCOS的问题COCOS的问题
    • ¥15 FPGA-SRIO初始化失败
    • ¥15 MapReduce实现倒排索引失败
    • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
    • ¥15 找一位技术过硬的游戏pj程序员
    • ¥15 matlab生成电测深三层曲线模型代码
    • ¥50 随机森林与房贷信用风险模型