...

View Full Version : Resolved HTTP Post With Curl instead of fopen



dniwebdesign
11-07-2010, 09:32 AM
So I've been working with my development server (which in turn has fopen working) and uploaded my script to my public server and remembered... fopen doesn't work as it is disabled.

Below I have the following function (and associated code to call it) to submit a call to Canada Post's Sell Online feature to calculate shipping. However, I need the function "do_post_request" to do it in curl and not fopen, but am unsure of how to do this. Any help would be appreciated.


function do_post_request($url, $data, $optional_headers = null)
{
$params = array('http' => array(
'method' => 'POST',
'content' => $data
));
if ($optional_headers !== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp) {
return "Problem with $url, $php_errormsg";

}
$response = @stream_get_contents($fp);
if ($response === false) {
return "Problem reading data from $url, $php_errormsg";

}
return $response;
}


$xmlDocument = '<?xml version="1.0" ?><eparcel>
<!--********************************-->
<!-- Prefered language for the -->
<!-- response (FR/EN) (optional) -->
<!--********************************-->
<language>en</language>

<ratesAndServicesRequest>
<!--**********************************-->
<!-- Merchant Identification assigned -->
<!-- by Canada Post -->
<!-- -->
<!-- Note: Use \'CPC_DEMO_HTML\' or ask -->
<!--**********************************-->
<merchantCPCID> CPC_DEMO_HTML </merchantCPCID>

<!--*********************************-->
<!--Origin Postal Code -->
<!--This parameter is optional -->
<!--*********************************-->
<fromPostalCode>S0E0L0</fromPostalCode>

<!--**********************************-->
<!-- Turn Around Time (hours) -->
<!-- This parameter is optional -->
<!--**********************************-->
<!--<turnAroundTime> 168 </turnAroundTime>-->

<!--**********************************-->
<!-- Total amount in $ of the items -->
<!-- for insurance calculation -->
<!-- This parameter is optional -->
<!--**********************************-->
<itemsPrice>0.00</itemsPrice>

<!--**********************************-->
<!-- List of items in the shopping -->
<!-- cart -->
<!-- Each item is defined by : -->
<!-- - quantity (mandatory) -->
<!-- - size (mandatory) -->
<!-- - weight (mandatory) -->
<!-- - description (mandatory) -->
<!-- - ready to ship (optional) -->
<!--**********************************-->
<lineItems>'.$_SESSION["xmlItems"].'



</lineItems>

<!--********************************-->
<!-- City where the parcel will be -->
<!-- shipped to -->
<!--********************************-->
<city>Carrot River</city>

<!--********************************-->
<!-- Province (Canada) or State (US)-->
<!-- where the parcel will be -->
<!-- shipped to -->
<!--********************************-->
<provOrState>Saskatchewan</provOrState>

<!--********************************-->
<!-- Country or ISO Country code -->
<!-- where the parcel will be -->
<!-- shipped to -->
<!--********************************-->
<country>CANADA</country>

<!--********************************-->
<!-- Postal Code (or ZIP) where the -->
<!-- parcel will be shipped to -->
<!--********************************-->
<postalCode>S0E 0L0</postalCode>
</ratesAndServicesRequest>
</eparcel>';


$data = array('xmlRequest'=>$xmlDocument);

$data = http_build_query($data);

include 'clsParseXML.php';

$xmlparse = &new ParseXML;
$xml = $xmlparse->GetXMLTree(do_post_request("http://sellonline.canadapost.ca:30000",$data));

$cpc_product = $xml["EPARCEL"][0]["RATESANDSERVICESRESPONSE"][0]["PRODUCT"];
$price_handling = $xml["EPARCEL"][0]["RATESANDSERVICESRESPONSE"][0]["HANDLING"][0]["VALUE"];
$products = count($cpc_product);




and so on....

dniwebdesign
11-07-2010, 05:50 PM
Figured it out...
Took a little while but here is the code I found.


<?php
function do_post_request($url,$data,$optional_headers = NULL) {

set_time_limit(10);
$ch = curl_init("sellonline.canadapost.ca:30000");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, "sellonline.canadapost.ca:30000");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 2678400);
curl_setopt( $ch, CURLOPT_HEADER, 0 );

//curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
$buf = "";
$buf = curl_exec($ch);

curl_close( $ch );
// echo $buf;
if($buf == ""){
$buf = '<?xml version="1.0" ?><eparcel><error><statusMessage>Cannot reach Canada Post Server. You may refresh this page (Press F5) to try again.</statusMessage></error></eparcel>';
}
// echo "<pre>";
// print_r($params["http"]["content"]);
return $buf;
} ?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum