[英]pecl_http add post request body in hotelbeds apitude
Recently I started working with hotelbeds apitude PHP API 最近我开始使用hotelbeds apitude PHP API
I am trying to add xml
code to a POST
request body using pecl_http
. 我正在尝试使用
pecl_http
将xml
代码添加到POST
请求正文中。 I tried with following code - 我尝试使用以下代码 -
$xml_part = <<< EOD
<<<XML PART>>> EOD;
$request = new http\Client\Request("POST",
$endpoint,
["Api-Key" => $hotel_beds_config['api_key'],
"X-Signature" => $signature,
"Content-Type" => "application/xml",
"Accept" => "application/xml"],
$xml_part
);
I got the following error 我收到以下错误
Fatal error: Uncaught TypeError: Argument 4 passed to http\\Client\\Request::__construct() must be an instance of http\\Message\\Body, string given
致命错误:未捕获TypeError:传递给http \\ Client \\ Request :: __ construct()的参数4必须是http \\ Message \\ Body的实例,给出字符串
Then I tried with following code - 然后我尝试使用以下代码 -
$request = new http\Client\Request("POST",
$endpoint,
["Api-Key" => $hotel_beds_config['api_key'],
"X-Signature" => $signature,
"Content-Type" => "application/xml",
"Accept" => "application/xml"],
new http\Message\Body($xml_part)
Now I get following error - 现在我得到以下错误 -
Fatal error: Uncaught http\\Exception\\InvalidArgumentException: http\\Message\\Body::__construct() expects parameter 1 to be resource, string given
致命错误:未捕获http \\ Exception \\ InvalidArgumentException:http \\ Message \\ Body :: __ construct()期望参数1为资源,给定字符串
I get the documentation to add body message here - 我在这里获得了添加正文消息的文档 -
How can I add the xml code to the POST
request? 如何将xml代码添加到
POST
请求中?
According to the http\\Message\\Body::__construct()
constructor documentation , the single parameter it optionally accepts is a stream or file handle resource (as in fopen()
). 根据
http\\Message\\Body::__construct()
构造函数文档 ,它可选择接受的单个参数是流或文件句柄资源(如fopen()
)。 It does not directly accept string data as you ave provided in $xml_part
. 它不像您在
$xml_part
提供的那样直接接受字符串数据。
Instead, pecl http provides an append()
method on the Body
class which you should be able to use to append the XML onto the empty body. 相反,pecl http在
Body
类上提供了一个append()
方法 ,您应该可以使用该方法将XML附加到空体上。 So start by creating a Body
object in a variable, then append your XML onto it. 首先在变量中创建一个
Body
对象,然后将XML附加到它上面。 Finally, pass the Body
object into your Request
object. 最后,将
Body
对象传递给Request
对象。
// Create a Body object first, with no argument
$body = new http\Message\Body();
// Append your XML to it
$body->append($xml_part);
// Create the Request and pass $body to it
$request = new http\Client\Request("POST",
$endpoint,
["Api-Key" => $hotel_beds_config['api_key'],
"X-Signature" => $signature,
"Content-Type" => "application/xml",
"Accept" => "application/xml"],
// Pass in $body
$body
);
// Create an \http\Client object, enqueue, and send the request...
$client = new \http\Client();
// Set options as needed for your application...
$client->enqueue($request);
$client->send();
The API you are attempting to POST to provides a PHP SDK . 您尝试POST的API 提供了PHP SDK 。 If the SDK supports the Availability function you are hoping to use, perhaps it would be simpler to use it rather than pecl_http (which has limited documentation).
如果SDK支持您希望使用的可用性功能,那么使用它可能更简单,而不是pecl_http(其文档有限)。 The SDK would then abstract all of the HTTP messaging into a series of PHP methods and properties, eliminating any doubt on the proper construction of POST requests.
然后,SDK会将所有HTTP消息传递抽象为一系列PHP方法和属性,从而消除对正确构造POST请求的任何疑问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.