Page 1 of 1

displaying response text after xml request using curl in php

Posted: Wed Feb 13, 2013 3:39 am
by njain1985
Hi,

I am stuck with something that is very important to fix asap.
I basically want to display the content of the response received
after xml request post in CURL. I am able to receive several info
regarding the request. However, I do not find any reference to
display the content of the received response. I know that the length
of the xml response is 1793. Which I verified manually in notepad++.
However, more importantly I wish to echo/print_r the response in php
webpage. Please help.

My codes are as per below:

<?php

$requestXML = "<XML><header><code>SAMPLEFIN6</code><version>0</version><count>1</count><timesent>20120518122033</timesent></header><data><startdate>20110101</startdate><enddate>20110131</enddate><airport>TESTAIRPORT</airport></data></XML>";

$server = 'http://123.123.123.123:8002';
$server = 'http://localhost:8002';
$headers = array(
"Content-type: text/xml"
,"Content-length: ".strlen($requestXML)
,"Connection: close"
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $server);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestXML);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = curl_exec($ch);
$result = curl_getinfo($ch);
$httpResponseCode = $result['http_code'];

if(curl_errno($ch)){
print curl_error($ch);
echo " something went wrong..... try later";
}else{
curl_close($ch);

//echo 'hiiii';
echo '<pre>';
print_r($result);
echo '</pre >';
}

echo 'The response size is: '.$result[download_content_length];

?>

Re: displaying response text after xml request using curl in

Posted: Wed Feb 13, 2013 4:01 am
by requinix
The CURLOPT_RETURNTRANSFER setting enabled

Code: Select all

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
there means that your curl_exec()

Code: Select all

$data = curl_exec($ch);
will return the response rather than output it. So it's $data you should be looking at.

Re: displaying response text after xml request using curl in

Posted: Wed Feb 13, 2013 6:00 pm
by mecha_godzilla
Hi,

For what it's worth, here's the code I use if I want to retrieve an RSS feed:

Code: Select all

echo 'Retrieving RSS feed...';

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, 'http://uk.php.net/releases.atom');
curl_setopt($ch, CURLOPT_TIMEOUT, 200);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);

if(curl_exec($ch) === false) {
echo 'Transfer failed' . '<br />';
} else {
echo 'Transfer successful' . '<br />';
echo 'Total number of bytes downloaded: ' . curl_getinfo($ch,CURLINFO_SIZE_DOWNLOAD) . '<br />';
echo 'Total size of all headers received: ' . curl_getinfo($ch,CURLINFO_HEADER_SIZE) . '<br />';
}

curl_close($ch);

var_dump($result);
HTH,

Mecha Godzilla