Web API - 包含标题和正文的HTTP发布请求

问题描述 投票:0回答:1

我只是获得了互联网上的一个网站的API访问权限,作为一个新的api开发人员,我遇到了麻烦,我应该如何开始和使用什么,所以希望你们指导我。他们还没有文件或教程。

如果有人能给我一个关于如何发送包含Header和body的Http post请求的小例子?正如他们在API页面中提到的那样:

所有请求必须包含带有siteid和apikey的Authorization标头(中间带冒号),并且必须与请求正文中的siteid和apikey匹配

在主体中,Parameter内容类型将是application / json。他们还提供了一个基本URL。

响应将作为application / json。

我该怎么办?请求是否可以使用AJAX发送?还是有PHP代码?我一直在阅读很多关于这个主题但没有人进入我的脑海。真的希望你们能帮助我。

如果您需要更多信息,请告诉我,以便我可以提供给您。

编辑:问题解决了,只是将我做的小编辑发布到我标记的正确答案中提供的代码。

感谢Anonymous先生给予我的大帮助。他的回答如此接近,我所要做的只是编辑他的代码,一切都很顺利。

我将列出下面的最终代码,以防任何其他开发人员遇到此问题或想要执行HTTP请求。

首先,我所做的是将我想要通过HTTP发送的数据存储在具有JSON类型的文件中:

{
  "criteria": {
    "landmarkId": 181,
    "checkInDate": "2018-02-25",
    "checkOutDate": "2018-02-30"
  }
}

第二件事是什么人可以看到Anonymous先生发布的内容。

<?php 
header('Access-Control-Allow-Origin: *');

$SiteID= 'My Site Id';
$ApiId= 'My Api Id';
$url = 'Base URL';

// Here i will get the data that i created in Json data type
$data = file_get_contents("data.json");

// I guess this step in not required cause the data are already in JSON but i had to do it for myself  
$arrayData = json_decode($data, true);

$ch = curl_init();
//curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$SiteID:$ApiID");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                                            'Content-Type: application/json',
                                            'Connection: Keep-Alive',
                                            'Authorization: $SiteID:$ApiId'
                                            ));
curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($arrayData)); 
$result = curl_exec($ch);
curl_close($ch);  
print_r($result); 

?>
php ajax api http http-post
1个回答
2
投票

试试这个

    $site_id = 'your_site_id';
    $api_key = 'your_api_key';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://api-connect-url');
    curl_setopt($ch, CURLOPT_POST, 1);

    ############  Only one of the statement as per condition ########### 

   //if they have asked for post
    curl_setopt($ch, CURLOPT_POSTFIELDS, "$site_id=$api_key" );

    //or if they have asked for raw post
    curl_setopt($ch, CURLOPT_POSTFIELDS, "$site_id:$api_key" );

    ####################################################################

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ["$site_id:$api_key"] );

    $api_response = curl_exec ($ch);

    curl_close ($ch);

由于提问者需要将JSON Payload发送给API

    $site_id = 'your_site_id';
    $api_key = 'your_api_key';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://api-connect-url');
    curl_setopt($ch, CURLOPT_POST, 1);

    //send json payload
    curl_setopt($ch, CURLOPT_POSTFIELDS, "{
        "criteria":{
            "cityId":9395,
            "area":{
                "id":0,
                "cityId":0
            },
            "landmarkId":0,
            "checkInDate":"2017-09-02",
            "checkOutDate":"2017-09-03",
            "additional":{
                "language":"en-us",
                "sortBy":"PriceAsc",
                "maxResult":10,
                "discountOnly":false,
                "minimumStarRating":0,
                "minimumReviewScore":0,
                "dailyRate":{
                    "minimum":1,
                    "maximum":10000
                },
                "occupancy":{
                    "numberOfAdult":2,
                    "numberOfChildren":1
                },
                "currency":"USD"
                }
            }
        }"
    );

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ["$site_id:$api_key", 'Content-Type:application/json'] );

    $api_response = curl_exec ($ch);

    curl_close ($ch);
© www.soinside.com 2019 - 2024. All rights reserved.