简体   繁体   English

如何在PHP中将REST身份和标题发布到RESTFUL API?

[英]How to post JSON body and header to RESTFUL API in php?

Kindly help me with, how to post JSON Body and header to RESTFUL API in php? 请帮助我,如何在PHP中发布JSON Body和标题到RESTFUL API? of following data. 以下数据

 HTTP POST url : http://webapi.test.com/Bus/BlockSeat
 ConsumerKey : KEY
 ConsumerSecret: SECRETKEY

 bODY(json) : {"IsOfflineBooking": "false",
"TripId": "5927-0",
"BoardingId": "6",
"NoofSeats": "1",
"Fares": "717.5",
"SeatNos": "R5",
 "Titles": "Mr",
 "Names": "vijaykumar",
 "Ages": "27",
 "Genders": "M",
 "Address": "hyderabad",
 "UserType": "5",
 "Servicetax": "0"} 

Kindly help with solving my problem. 请帮助解决我的问题。

Here is your code, using CURL : 这是您的代码,使用CURL:

$body = json_encode(array(
    "IsOfflineBooking" => "false",
    "TripId" => "5927-0",
    "BoardingId" => "6",
    "NoofSeats" => "1",
    "Fares" => "717.5",
    "SeatNos" => "R5",
    "Titles" => "Mr",
    "Names" => "vijaykumar",
    "Ages" => "27",
    "Genders" => "M",
    "Address" => "hyderabad",
    "UserType" => "5",
    "Servicetax" => "0"
));

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://webapi.test.com/Bus/BlockSeat");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                        
    'Content-Length: '.strlen($body)                                                                              
    'ConsumerKey: '.KEY,
    'ConsumerSecret: '.SECRETKEY                                                                        
));  

$output = curl_exec($ch);

curl_close($ch);

And do what you want with the respons in $output 并以$output的响应做你想做的事

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM