JSON POST请求 - JSON数据,如何在具有代理请求的跨服务器上使用JSON格式的数据执行发布请求

时间:2013-10-07 11:54:59

标签: php json api curl

我使用json格式使用以下API调用,但我没有获得任何格式的输出。 我想知道的是我如何使用javacript设置代理代码,以便我可以从跨服务器(另一台服务器)获取数据。 是否有其他可能从外国域接收/显示数据? iFrame遵循相同的政策? 这是我的api url: api.wego.com/flights/api/k/2/searches?api_key=your_secret_api_key&ts_code=your_secret_ts_code

我希望传递以下参数:     {       “旅行”:[         {           “departure_code”:“SIN”,           “arrival_code”:“HKG”,           “outbound_date”:“2013-10-14”,           “inbound_date”:“2013-10-21”         }       ]       “adults_count”:1     }

我正在使用以下代码:

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_URL, 'http://api.wego.com/flights/api/k/2/searches?api_key=xxxxxxxxx&ts_code=xxxxx');
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type: application/json','Accept: application/json'));

$data = 
"trips": [
    {
      "departure_code": "SIN",
      "arrival_code": "HKG",
      "outbound_date": "2013-10-14",
      "inbound_date": "2013-10-21"
    }
  ],
  "adults_count": 1;

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$result=curl_exec($ch);
var_dump($result); 

由于

1 个答案:

答案 0 :(得分:0)

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_URL, 'http://api.wego.com/flights/api/k
      /2/searches?api_key=xxxxxxxxx&ts_code=xxxxx');
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type'=>'application/json',
        'Accept'=> 'application/json'));

$data ='{"trips":
     [ { "departure_code": "SIN", "arrival_code": "HKG",
           "outbound_date": "2013-10-14", "inbound_date": "2013-10-21" } ]
     , "adults_count": 1}';

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result=curl_exec($ch);
var_dump($result);