Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    844
    Thanks
    15
    Thanked 9 Times in 9 Posts

    HTTP Post With Curl instead of fopen

    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.

    PHP Code:
     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....
    Last edited by dniwebdesign; 11-07-2010 at 09:55 PM.
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #2
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    844
    Thanks
    15
    Thanked 9 Times in 9 Posts
    Figured it out...
    Took a little while but here is the code I found.

    PHP Code:
    <?php 
    function do_post_request($url,$data,$optional_headers NULL) {

                    
    set_time_limit(10);
                    
    $ch curl_init("sellonline.canadapost.ca:30000");
                    
    curl_setopt($chCURLOPT_RETURNTRANSFER1);
                    
    curl_setopt($chCURLOPT_URL"sellonline.canadapost.ca:30000");
                    
    curl_setopt($chCURLOPT_SSL_VERIFYPEER0);
                    
    curl_setopt($chCURLOPT_SSL_VERIFYHOST0);
                    
                    
    curl_setopt($chCURLOPT_DNS_CACHE_TIMEOUT2678400);
                    
    curl_setopt$chCURLOPT_HEADER);
                                                           
                    
    //curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
                    
    curl_setopt($chCURLOPT_POSTFIELDS$data);
                    
    curl_setopt$chCURLOPT_RETURNTRANSFER);
                    
    $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;
    ?>
    Last edited by dniwebdesign; 11-07-2010 at 09:55 PM. Reason: Solved
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •