dongluo3962 2015-07-03 09:46
浏览 33
已采纳

PHP JSON请求

Hi I am trying to make JSON Request to a API but I am not sure how to as I have never worked on something similar ever before. I would really appreciate if someone can help me please.

Below is the request that I need to make:

request payload:

{
    "sessionId": "1234567890",
    "availabilityRequest": {
        "checkInDate": "29042014",
        "checkOutDate": "",
        "noRooms": 1,
        "noNights": 1,
        "userType": "leisure",
        "rateType": "standard",
        "roomPreference": [
            {
                "noAdult": 1,
                "noChild": 0
            }
        ],
        "siteCode": [
            "GB0758",
            "GB0746",
            "GB0738",
            "GB0755",
            "GB0742"
        ],
        "includeDisabled": "F"
    }
}

This is what I have done but I am getting error Array ( [error] => Array ( [code] => 4007 [message] => Invalid JSON POST data (unable to decode): ) )

       $postData = '{
                "sessionId":"1234567890",
                "availabilityRequest":
                {
                    "checkInDate": "29042014",
                    "checkOutDate": "",
                    "noRooms": 1,
                    "noNights": 1,
                    "userType": "leisure",
                    "rateType": "standard",
                    "roomPreference": 
                        [
                            { "noAdult":1, "noChild":0 }
                        ],
                    "siteCode": 
                        [
                        "GB0758","GB0746","GB0738","GB0755","GB0742"
                        ],
                    "includeDisabled":"F"
                }
            }';

        $ch = curl_init($url);
        curl_setopt_array($ch, array(
        CURLOPT_POST => TRUE,
        CURLOPT_RETURNTRANSFER => TRUE,
        CURLOPT_POSTFIELDS => $postData));

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        // Send the request
        $response = curl_exec($ch);

        // Check for errors
        if($response === FALSE){
            die(curl_error($ch));
        }

        // Decode the response
        $responseData = json_decode($response, TRUE);

        // Print the date from the response
        print_r($responseData);

I would be really grateful is someone can help me please. Thank you

  • 写回答

2条回答 默认 最新

  • doufei16736 2015-07-03 12:05
    关注

    I can't really test this because I have no URL to test it with, but you can try to set a header.

    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($postData)));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示