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