Как отправить xml сообщение методом POST
1 ответ 1
У вас должна быть документация по данной API банка в ней есть все примеры как и чем лучше отправлять, если ее у вас нет запросите ее у банка или загуглите возможно она есть в открытом доступе.
Обычно у для такого запроса нужна регистрация в банке и получение закрытого уникального токена доступа и обычно для таких вот запросов банки пишут API на SOAP .
Если получить документацию не удалось то попробовать можно cURL PHP
Пример кода для отправки XML через POST :
$input_xml = ‘ #123
#1222
PHP: Send XML via POST.
This is a guide on how to send XML via a POST request using PHP. In this tutorial, I will be using the inbuilt cURL functions to send the request.
Let’s jump right in and take a look at the following code:
//The XML string that you want to send. $xml = ''; //The URL that you want to send your XML to. $url = 'http://localhost/xml'; //Initiate cURL $curl = curl_init($url); //Set the Content-Type to text/xml. curl_setopt ($curl, CURLOPT_HTTPHEADER, array("Content-Type: text/xml")); //Set CURLOPT_POST to true to send a POST request. curl_setopt($curl, CURLOPT_POST, true); //Attach the XML string to the body of our request. curl_setopt($curl, CURLOPT_POSTFIELDS, $xml); //Tell cURL that we want the response to be returned as //a string instead of being dumped to the output. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); //Execute the POST request and send our XML. $result = curl_exec($curl); //Do some basic error checking. if(curl_errno($curl)) < throw new Exception(curl_error($curl)); >//Close the cURL handle. curl_close($curl); //Print out the response output. echo $result; 2019-20-02T10:45:00 Meeting Team meeting in Boardroom 2A.
- We assigned our XML string to a PHP variable.
- We initiated cURL and specified the URL that we want to send our XML data to.
- Using the CURLOPT_HTTPHEADER option, we set the Content-Type header to text/xml. This may or may not be required depending on the service that you are sending your POST request to.
- In order to tell cURL that we want to send a POST request, we set the CURLOPT_POST option to TRUE. By default, cURL will send a GET request.
- We assigned our XML data to the body of the request.
- We told cURL to return the response output as a variable by setting the CURLOPT_RETURNTRANSFER option to TRUE.
- Finally, we executed the request and printed out the response. We also added in some basic error checking, just in case the request fails.
Send XML without cURL.
If you do not have access to the cURL functions, then you can check out my guide on sending a POST request without cURL.
Note that you may need to modify the function and change the header value on LINE 17. You may also need to remove the http_build_query function on LINE 11. This will all depend on the service you are sending your XML to.
Send XML file data via cURL.
If your XML content is located in a file, then you can read the contents of that file using the file_get_contents function:
//Read the XML file in as a string. $xml = file_get_contents('file.xml');
In the example above, the file_get_contents function reads in XML data from a file called file.xml and assigns it to the variable $xml. This code would replace the XML assignment on LINE 2 of the original example above.
Hopefully, you found this post to be helpful!
Related article: Receive XML via POST.
Send XML data to webservice using php curl
I’m working on Flight API of arzoo. The server must receive the posted data in simple POST Request. To achieve this i’m using PHP cURL. In the API Document it is clearly mention that the data should be sent in the following format:
ONE BOM NYC 2013-09-15 2013-09-16 1 0 0 INR E true 77752369 *AB424E52FB5ASD23YN63A099A7B747A9BAF61F8E ArzooINTLWS1.0
$URL = "http://59.162.33.102:9301/Avalability"; //setting the curl parameters. $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$URL); curl_setopt($ch, CURLOPT_VERBOSE, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml')); curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); if (curl_errno($ch)) < // moving to display page to display curl errors echo curl_errno($ch) ; echo curl_error($ch); >else < //getting response from server $response = curl_exec($ch); print_r($response); curl_close($ch); >
I’m not getting anything in response. I’ve spoken about the same with the API Provider but they found empty request in their log. Am i missing something from my end. Your reply will be appreciated. Thank You.
Try to execute the request before checking errors, something like: if(curl_exec($ch) === false) echo ‘Curl error: ‘ . curl_error($ch);
Hi Lucas, Thank you for your time and reply. I did that earlier and this time also: $response = curl_exec($ch); if($response == false) < echo 'Curl error: ' . curl_error($ch); echo 'Curl errorno: ' . curl_errno($ch); >else < print_r($response); curl_close($ch); >:: This code outputs me: Curl error: Curl errorno: 0. I don’t know why is it going in if loop.
You can use strict comparison for testing your response if($response === false) . Curl error 0 means no error, so you still have an empty response that evaluate to false with == . I made the same request with curl command line and just get a 200 OK with empty body.
@Lucas:Thanks for your kind reply, I could probably solve this problem by your solutions and by doing another trial and error. I really appreciate your ample time given on this problem. at hakre: Thank for your cooperation and looking after the stated problem. The link shared in above comment is exactly similar with the problem i was facing with. I got the response from server by simply adding curl_setopt($ch, CURLOPT_POSTFIELDS,»xmlRequest=» . $input_xml); and Last json code to decode the response. Thank you both of you. I really appreciate your kind work and support.
5 Answers 5
After Struggling a bit with Arzoo International flight API, I’ve finally found the solution and the code simply works absolutely great with me. Here are the complete working code:
//Store your XML Request in a variable $input_xml = ' ONE BOM JFK 2013-09-15 2013-09-16 1 0 0 INR E true 777ClientID *Your API Password ArzooINTLWS1.0 ';
Now I’ve made a little changes in the above curl_setopt declaration as follows:
$url = "http://59.162.33.102:9301/Avalability"; //setting the curl parameters. $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); // Following line is compulsary to add as it is: curl_setopt($ch, CURLOPT_POSTFIELDS, "xmlRequest mt24">)" data-controller="se-share-sheet" data-se-share-sheet-title="Share a link to this answer" data-se-share-sheet-subtitle="" data-se-share-sheet-post-type="answer" data-se-share-sheet-social="facebook twitter devto" data-se-share-sheet-location="2" data-se-share-sheet-license-url="https%3a%2f%2fcreativecommons.org%2flicenses%2fby-sa%2f3.0%2f" data-se-share-sheet-license-name="CC BY-SA 3.0" data-s-popover-placement="bottom-start">Shareanswered Sep 9, 2013 at 21:22 user1549991user15499919092 gold badges8 silver badges11 bronze badges