I'm new to sending, receiving, and reading XML. I'm trying to send and receive XML data via cURL. I am using curl because it was so easy with just arrays. I am using the following code to generate the XML and the PHP script that is presently not working. It would be great if some great code god can explain whats wrong to me. build_xml.php <?php '<?xml version="1.0"?>' $xml = "<member><name>name</name></member>"; echo $xml; ?> Code (markup): Array Method - Works great with arrays. I tried with XML as $data, and it no work. Getting it working this way is preferred. <?php exec("/usr/bin/curl -m 120 -d \"$xml\" http://www.example.com/index.php -L", $response); // $response is null ?> Code (markup): Alternate Method - This way is failing also. <?php $url = "build_xml.php"; $ch = curl_init(); // initialize curl handle curl_setopt($ch, CURLOPT_URL,$url); // set url to post to curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable curl_setopt($ch, CURLOPT_TIMEOUT, 4); // times out after 4s curl_setopt($ch, CURLOPT_POSTFIELDS, $XPost); // add POST fields $result = curl_exec($ch); // run the whole process echo $result; //contains response from server // NO RESPONSE RECEIVED ?> Code (markup):
<?php /** * Define POST URL and also payload */ define('XML_PAYLOAD', '<?xml version="1.0"?><member><name>name</name></member>'); define('XML_POST_URL', 'http://www.domain.com/build_xml.php'); /** * Initialize handle and set options */ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, XML_POST_URL); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 4); curl_setopt($ch, CURLOPT_POSTFIELDS, XML_PAYLOAD); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close')); /** * Execute the request and also time the transaction */ $start = array_sum(explode(' ', microtime())); $result = curl_exec($ch); $stop = array_sum(explode(' ', microtime())); $totalTime = $stop - $start; /** * Check for errors */ if ( curl_errno($ch) ) { $result = 'ERROR -> ' . curl_errno($ch) . ': ' . curl_error($ch); } else { $returnCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE); switch($returnCode){ case 404: $result = 'ERROR -> 404 Not Found'; break; default: break; } } /** * Close the handle */ curl_close($ch); /** * Output the results and time */ echo 'Total time for request: ' . $totalTime . "\n"; echo $result; /** * Exit the script */ exit(0); ?> PHP:
Note: the error checking section should actually look something like this: /** * Check for errors */ if ( curl_errno($ch) ) { $result = 'cURL ERROR -> ' . curl_errno($ch) . ': ' . curl_error($ch); } else { $returnCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE); switch($returnCode){ case 200: break; default: $result = 'HTTP ERROR -> ' . $returnCode; break; } } PHP: Notice the difference is in the switch so it captures the non-200 response code. Bobby
wow it works great. thanks so much. i did notice that it only accepts one reply value. can it receive an xml data set like its sending? otherwise its perfect. great solution.
I've tried using this solution and the xml comes over as an array. Is that what we should be expecting. As I'm sending to a third party its hard to know if the issue may be on their end. (I'm hoping someone is still getting replies to this thread.
thanks for ur reply........ we have already got through the firewall for our corresponding ip its my code <?php /** * Define POST URL and also payload */ define('XML_PAYLOAD', '<?xml version="1.0" encoding="UTF-16" standalone="no"?> <data> <ID>27</ID> <REQ_ID>0123456789abcdefg</REQ_ID> <ReferenceNo>29684</ReferenceNo> <ProcessDate>02/27/12</ProcessDate> <ProcessTime>11:18:20 AM</ProcessTime> </data>'); define('XML_POST_URL', 'http://96.253.63.114:25080/msmqdci/receiver.asp'); /** * Initialize handle and set options */ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, XML_POST_URL); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 4); curl_setopt($ch, CURLOPT_POSTFIELDS, XML_PAYLOAD); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close')); /** * Execute the request and also time the transaction */ $start = array_sum(explode(' ', microtime())); $result = curl_exec($ch); $stop = array_sum(explode(' ', microtime())); $totalTime = $stop - $start; /** * Check for errors */ if ( curl_errno($ch) ) { $result = 'ERROR -> ' . curl_errno($ch) . ': ' . curl_error($ch); } else { $returnCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE); switch($returnCode){ case 404: $result = 'ERROR -> 404 Not Found'; break; default: break; } } /** * Close the handle */ curl_close($ch); /** * Output the results and time */ echo 'Total time for request: ' . $totalTime . "\n"; echo $result; /** * Exit the script */ exit(0); ?>
what site are you receiving information responses from? I know UPS has an XML barebones that can be interfaced with PHP in a few simple steps.