Php send xml post

Как отправить xml сообщение методом POST

1 ответ 1

У вас должна быть документация по данной API банка в ней есть все примеры как и чем лучше отправлять, если ее у вас нет запросите ее у банка или загуглите возможно она есть в открытом доступе.
Обычно у для такого запроса нужна регистрация в банке и получение закрытого уникального токена доступа и обычно для таких вот запросов банки пишут API на SOAP .
Если получить документацию не удалось то попробовать можно cURL PHP
Пример кода для отправки XML через POST :
$input_xml = ‘ 1 http://www.logo.ru/logotype.png 1111111111 http://ulmart.ru/my_endpoint RFE003453PO 0 15 ILEF AABB01_IS1 Арсентьев Антон Андреевич 1212 123456 lol4e@gmail.com 9197262902 #123 Samsung 1 25000 10/10/10 #1222 HTC 2 15000 http://www.photo.ru/product.png © 2001–2016 Альфа-Банк 8 ‘; $url = «https://anketa.alfabank.ru/alfaform-pos/endpoint»; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POSTFIELDS,»xmlRequest mt24″>

Источник

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 = '  2019-20-02T10:45:00 Meeting Team meeting in Boardroom 2A. '; //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;
  1. We assigned our XML string to a PHP variable.
  2. We initiated cURL and specified the URL that we want to send our XML data to.
  3. 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.
  4. 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.
  5. We assigned our XML data to the body of the request.
  6. We told cURL to return the response output as a variable by setting the CURLOPT_RETURNTRANSFER option to TRUE.
  7. Finally, we executed the request and printed out the response. We also added in some basic error checking, just in case the request fails.
Читайте также:  Textarea Auto Height Based on Content Jquery - - nicesnippets.com

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">Share
answered Sep 9, 2013 at 21:22
2
    1
    For me, the code above doesn't work. Returning XML not valid. But following this answers did help stackoverflow.com/a/15679384/2990720
    – AlbertSamuel
    Nov 9, 2017 at 4:38
    I am newbie play around with xml, how to use the $input_xml dynamically. I mean the value came from a form and pass it using $POST?
    – Bireon
    Apr 24, 2019 at 8:24
Add a comment|
10

Previous anwser works fine. I would just add that you dont need to specify CURLOPT_POSTFIELDS as "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">Share
answered Oct 30, 2014 at 9:07
Add a comment|
4

Check this one. It will work.

function fetch($i1,$i2,$i3,$i4) < $input_data = ''.$i1.''.$i2.''.$i2.''.$i3.''; $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_PORT => "8080", CURLOPT_URL => "http://192.168.1.100:8080/avaliablity", CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "POST", CURLOPT_POSTFIELDS => $input_data, CURLOPT_HTTPHEADER => array( "Cache-Control: no-cache", "Content-Type: application/xml" ), )); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); if ($err) < echo "cURL Error #:" . $err; >else < echo $response; >> fetch('i1','i2','i3','i4');

If you are using shared hosting, then there are chances that outbound port might be disabled by your hosting provider. So please contact your hosting provider and they will open the outbound port for you

Send XML data form api using php curl and recive data in api endpoint useing php

create a abc.php and hit this page url

 XML; $data = new SimpleXMLElement($pruebaXml); $lead = $data->addChild('lead'); // data start $lead->addChild('key', 'adf'); $lead->addChild('leadgroup', 60301); $lead->addChild('site', 0); $lead->addChild('type', 'ad'); $lead->addChild('user', 'asdf'); $lead->addChild('status', 1); $lead->addChild('reference', 'sadf'); $lead->addChild('source', 'LRXS'); $lead->addChild('medium', 'asdf'); $lead->addChild('term', '60301'); $lead->addChild('cost', '60301'); $lead->addChild('value', '60301'); // data end $xml = $data->asXML(); $headers = [ 'Content-Type: text/xml' ]; // echo $xml;exit; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://localhost/xyz.php'); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $response = curl_exec($ch); curl_close($ch); $responseXml = simplexml_load_string($response); // echo $responseXml->message; print_r($response) ?> 

after that api end point in use this code and Recive all xml data

loadXML($xmlData); $name = $xml->getElementsByTagName('name')->item(0)->nodeValue; $age = $xml->getElementsByTagName('age')->item(0)->nodeValue; $response = new SimpleXMLElement(''); $response->addChild('status', '200'); $response->addChild('Msg', 'Data Recived'); $response->addChild('message', $name); $response->addChild('message', $age); echo $response->asXML(); ?> 

Источник

Send an XML post request to a web server with CURL

I am trying to send a request to a web server using php and curl. I haven't done something like this before and although there are many nice examples online I have some difficulties understanding some of the curl commands. This is what I want to do: There is an established web service (for example: Web map service) and I want my php code to send a post XML request to this service. As a respond I want to get an XML file. This is what I have till now:

 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, ''); /*curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));*/ /* curl_setopt($ch, CURLOPT_HEADER, 0);*/ curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); /*curl_setopt($ch, CURLOPT_REFERER, '');*/ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $ch_result = curl_exec($ch); curl_close($ch); echo $ch_result; 

As I said I am quite new in php and also in using curl and I think I am missing some concepts. My questions are: 1) What is the string (link) that I have to put in the:

 curl_setopt($ch, CURLOPT_URL, ''); 

Is it the host name of the service which I want to send the request? 2) In row 6 the variable $xml contains the xml file that I want to send as a request. Is it correct or this variable is supposed to contain something else? 3) In which cases do I need to use a httpheader or header (row3 and row4); Thanks for the help. Dimitris

Источник

Оцените статью