cfyves Posted January 31, 2009 Share Posted January 31, 2009 Hi there, Recently, I've installed a Magento system... and I've installed a canada post shipping module for this system. One thing that seems to be tripping it up is a problem with the curl function.. curl is attempting to send xml data to canada post's server on port 30000. The response from canada post's system is always an html page... instead of corresponding xml data. I've run a test using stream_socket_client and have gotten it to work, the canada post's system responds with the corresponding XML data with shipping info. I'm wondering if there are any curlopt_ options that should be set that aren't currently set... or could it be that curl is not sending properly formatted XML. The xml file in the example is properly formatted, but could this script or the curl function be adding whitespace? This is the code I'm running.. very similar to the module: <?php try { $url = "http://sellonline.canadapost.ca"; $port = "30000"; // omitted the full path for example $filename = "cptest.xml"; $eparcelRequest = file_get_contents($filename); echo "Testing connection $url:$port"; $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_PORT, $port); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15); curl_setopt($ch, CURLOPT_POST,1); curl_setopt($ch, CURLOPT_POSTFIELDS, "XMLRequest=$eparcelRequest"); $responseBody = curl_exec($ch); $status = curl_getinfo($ch); curl_close ($ch); echo "<br />Request ended without error"; } catch (Exception $e) { echo "<br />Request ended with error<br/><pre>"; echo print_r($e,true)."</pre>"; $responseBody = 'error'; } if ($status['http_code']==200) { if (strpos(trim($responseBody), '<?xml')===0) { echo "<h1 style=\"color:green\">Passed</h1>"; } else { echo "<h1 style=\"color:red\">Failed</h1>"; echo "Able to communicate but did not receive a valid XML response<br />"; } } else { echo "<h1 style=\"color:red\">Failed to connect</h1>"; echo "Communication error<br />"; } echo "Details:<br /><pre>Response:\n"; echo htmlspecialchars ( $responseBody)."\n\nStatus\n"; echo print_r($status,true ); echo "</pre>"; /* */ ?> The curl_getinfo($ch) returns the following: ( => http://sellonline.canadapost.ca [content_type] => text/html [http_code] => 200 [header_size] => 206 [request_size] => 324 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.235005 [namelookup_time] => 7.4E-5 [connect_time] => 0.116565 [pretransfer_time] => 0.116678 [size_upload] => 0 [size_download] => 1650 [speed_download] => 7021 [speed_upload] => 0 [download_content_length] => 1650 [upload_content_length] => 0 [starttransfer_time] => 0.23497 [redirect_time] => 0 ) Any ideas? Thanks! Link to comment https://forums.phpfreaks.com/topic/143263-problem-with-curl-and-post-xml-data-on-a-particular-port/ Share on other sites More sharing options...
cfyves Posted February 1, 2009 Author Share Posted February 1, 2009 First off I'd like to thank the Magento module's author for the support. The canada post shipping module now works! The problem was a little bit different then I saw in other posts. Basically, the curl function wouldn't connect to the sellonline.canadapost.ca system on the desired port (30000). In the curl section of code, we set opts.. and in the opts... we set the URL and the port #. For some reason this would not work. curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_PORT, $port); So.... what ended up working was to add the port directly to the url and comment out the port option.. // basically this is what is being passed to the URL opt $url = 'http://sellonline.canadapost.ca:30000'; curl_setopt($ch, CURLOPT_URL, $url); // and comment out the port opt //curl_setopt($ch, CURLOPT_PORT, $port); So, after some diggin' around on curl.... and great support by module's author.... problem solved. Canada Post shipping module now works. BTW - SOLVED Link to comment https://forums.phpfreaks.com/topic/143263-problem-with-curl-and-post-xml-data-on-a-particular-port/#findComment-752160 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.