recently started working hotelbeds apitude php api
i trying add xml
code post
request body using pecl_http
. tried 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 following error
fatal error: uncaught typeerror: argument 4 passed http\client\request::__construct() must instance of http\message\body, string given
then tried 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 following error -
fatal error: uncaught http\exception\invalidargumentexception: http\message\body::__construct() expects parameter 1 resource, string given
i documentation add body message here -
how can add xml code post
request?
according http\message\body::__construct()
constructor documentation, single parameter optionally accepts stream or file handle resource (as in fopen()
). not directly accept string data ave provided in $xml_part
.
instead, pecl http provides an append()
method on body
class should able use append xml onto empty body. start creating body
object in variable, append xml onto it. finally, pass body
object request
object.
// create body object first, no argument $body = new http\message\body(); // append xml $body->append($xml_part); // create request , pass $body $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 \http\client object, enqueue, , send request... $client = new \http\client(); // set options needed application... $client->enqueue($request); $client->send();
addendum: why not use sdk?
the api attempting post provides php sdk. if sdk supports availability function hoping use, perhaps simpler use rather pecl_http (which has limited documentation). sdk abstract of http messaging series of php methods , properties, eliminating doubt on proper construction of post requests.
Comments
Post a Comment