[英]Web API - HTTP Post Request That Includes Header & Body
I just got API access for one of the website on the internet, and as a new api developer i got into troubles understanding how should i start and with what, So hope you guys guide me . 我刚刚可以访问Internet上一个网站的API,并且作为一个新的api开发人员,我在理解我应该如何开始以及从什么开始方面遇到了麻烦,因此希望你们能引导我。 They don't have Documents or tutorials Yet.
他们还没有文档或教程。
If anyone can give me a small example on how to send Http post request that includes Header and body? 如果有人可以给我一个关于如何发送包含Header和body的Http post请求的小例子? As what they are mentioning in the API page:
正如他们在API页面中提到的:
All requests must include an Authorization header with siteid and apikey (with a colon in between) and it must match with the siteid and apikey in the request body
所有请求都必须包含带有siteid和apikey(之间带有冒号)的Authorization标头,并且必须与请求正文中的siteid和apikey匹配
In the body the Parameter content type will be application/json. 在主体中,参数内容类型将为application / json。 They have also provided a Base URL.
他们还提供了基本URL。
The response will be as application/json. 响应将为application / json。
What should i do? 我该怎么办? is the request can be sent using AJAX?
可以使用AJAX发送请求吗? or there is PHP code for this?
或为此有PHP代码? i have been reading a lot about this subject but none enter my head.
我已经读了很多关于这个主题的书,但是没有一个进入我的脑海。 Really hope you guys help me out .
真的希望你们能帮助我。
Please let me know if you need more information so i can provide it to you. 如果您需要更多信息,请告诉我,以便我为您提供。
EDIT : Problem solved and just posting the small editing that i did to the code that was provided in the correct answer that i marked . 编辑:问题已解决,只是将我所做的小型编辑发布到我标记的正确答案中提供的代码中。
Thanks to Mr. Anonymous for the big help that he gave me . 感谢Anonymous先生给我的大力帮助。 His answer was so so close, all what i had to do is just edit his code a little bit and everything went good .
他的回答是如此接近,我所要做的就是只是稍微编辑一下他的代码,一切就一切顺利了。
I will list the finial code down bellow in case any other developer had this issue or wanted to do an HTTP request . 我将在下面列出最终代码,以防其他开发人员遇到此问题或想要执行HTTP请求。
First what i did is that i stored the data that i wanted to send over the HTTP in a file with a JSON type : 首先,我所做的是将要通过HTTP发送的数据存储在JSON类型的文件中:
{
"criteria": {
"landmarkId": 181,
"checkInDate": "2018-02-25",
"checkOutDate": "2018-02-30"
}
}
Second thing as what guys can see what Mr. Anonymous posted . 第二件事是,什么人可以看到匿名先生张贴的内容。
<?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);
?>
Try this 尝试这个
$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);
As asker need to send JSON Payload to API 由于要求将JSON有效负载发送到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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.