30

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:

<AvailRequest>
        <Trip>ONE</Trip>
        <Origin>BOM</Origin>
        <Destination>NYC</Destination>
        <DepartDate>2013-09-15</DepartDate>
        <ReturnDate>2013-09-16</ReturnDate>
        <AdultPax>1</AdultPax>
        <ChildPax>0</ChildPax>
        <InfantPax>0</InfantPax>
        <Currency>INR</Currency>
        <Preferredclass>E</Preferredclass>
        <Eticket>true</Eticket>
        <Clientid>77752369</Clientid>
        <Clientpassword>*AB424E52FB5ASD23YN63A099A7B747A9BAF61F8E</Clientpassword>
        <Clienttype>ArzooINTLWS1.0</Clienttype>
        <PreferredAirline></PreferredAirline>
</AvailRequest>

I've taken the above code in a variable $xml. My PHP cURL code is as follows:

$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.

user1549991
  • 909
  • 2
  • 8
  • 11
  • Try to execute the request before checking errors, something like: `if(curl_exec($ch) === false) echo 'Curl error: ' . curl_error($ch);` – LFI Sep 09 '13 at 11:59
  • 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. – user1549991 Sep 09 '13 at 12:56
  • 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. – LFI Sep 09 '13 at 13:52
  • 1
    possible duplicate of [How to fetch dynamical XML data from a URL](http://stackoverflow.com/questions/16298065/how-to-fetch-dynamical-xml-data-from-a-url) – hakre Sep 09 '13 at 17:33
  • The duplicate suggestion is in good faith. If you want to get an answer to your technical question, please provide reference to the API documentation of arzoo that you use there otherwise we can not say how the request needs to be formed to work. – hakre Sep 09 '13 at 17:38
  • 1
    @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. – user1549991 Sep 09 '13 at 18:04
  • @hakre Sure will do that right away. – user1549991 Sep 09 '13 at 21:25
  • psst: Perhaps tell which change(s) were necessary (next to the full code you've already added) compared with your question. ;) – hakre Sep 09 '13 at 21:40

5 Answers5

53

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 = '<AvailRequest>
            <Trip>ONE</Trip>
            <Origin>BOM</Origin>
            <Destination>JFK</Destination>
            <DepartDate>2013-09-15</DepartDate>
            <ReturnDate>2013-09-16</ReturnDate>
            <AdultPax>1</AdultPax>
            <ChildPax>0</ChildPax>
            <InfantPax>0</InfantPax>
            <Currency>INR</Currency>
            <PreferredClass>E</PreferredClass>
            <Eticket>true</Eticket>
            <Clientid>777ClientID</Clientid>
            <Clientpassword>*Your API Password</Clientpassword>
            <Clienttype>ArzooINTLWS1.0</Clienttype>
            <PreferredAirline></PreferredAirline>
    </AvailRequest>';

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=" . $input_xml);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
        $data = curl_exec($ch);
        curl_close($ch);

        //convert the XML result into array
        $array_data = json_decode(json_encode(simplexml_load_string($data)), true);

        print_r('<pre>');
        print_r($array_data);
        print_r('</pre>');

That's it the code works absolutely fine for me. I really appreciate @hakre & @Lucas For their wonderful support.

user1549991
  • 909
  • 2
  • 8
  • 11
  • 1
    For me, the code above doesn't work. Returning XML not valid. But following this answers did help https://stackoverflow.com/a/15679384/2990720 – AlbertSamuel Nov 09 '17 at 04: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 '19 at 08:24
10

Previous anwser works fine. I would just add that you dont need to specify CURLOPT_POSTFIELDS as "xmlRequest=" . $input_xml to read your $_POST. You can use file_get_contents('php://input') to get the raw post data as plain XML.

bpile
  • 359
  • 3
  • 10
4

Check this one. It will work.

function fetch($i1,$i2,$i3,$i4)
{
$input_data = '<I> 
                <i1>'.$i1.'</i1> 
                <i2>'.$i2.'</i2> 
                <i3>'.$i2.'</i3> 
                <i4>'.$i3.'</i4> 
              </I>';
$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');
ThunderStorm
  • 155
  • 1
  • 1
  • 12
1

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

0

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

<?php
$pruebaXml = <<<XML
<?xml version="1.0" encoding="UTF-8" ?>
<data>
</data>
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

<?php 
header('Content-Type: text/xml');
$xmlData = file_get_contents('php://input');
$xml = new DOMDocument();
$xml->loadXML($xmlData);
$name = $xml->getElementsByTagName('name')->item(0)->nodeValue;
$age = $xml->getElementsByTagName('age')->item(0)->nodeValue;


$response = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><response/>');
$response->addChild('status', '200');
$response->addChild('Msg', 'Data Recived');
$response->addChild('message', $name);
$response->addChild('message', $age);

echo $response->asXML();

?>        

                         
Anupam Verma
  • 49
  • 1
  • 3