Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 359
  • Last Modified:

Translate cURL to fsockopen

I just purchased some PHP code that is supposed to connect to UPS for the Online Address Validation Tool. The code, however, uses cURL. I don't want to install it. I just want to translate the code below into standard PHP socket code. Please help!!!

Thanks,

/*
** Verify an address
*/
function ups_address_verify($ship_to) {
/////////////////////////////////////////////////////////////////////////////////////////////
// NOTE: Characters such as "&" "<" ">" """ "'" have to be replaced in regard of the W3C
// definition of XML.  These characters will break the XML document if they are not replaced.
/////////////////////////////////////////////////////////////////////////////////////////////

/*The global $CFG variable should contain any persistant data for your application.
            
$CFG->ups_xml_access_key                  // Your UPS Access Key
$CFG->ups_userid                              // Your UPS User ID
$CFG->ups_password                              // your UPS Password
$CFG->ups_shipper_number                  // Your Shipper Number
$CFG->ups_testmode                              // TRUE for test transactions, FALSE for live
*/

global $CFG;

/* the following is a list of fields that are used by this function
$ship_to array - Where the shipment is shipping to
$ship_to["city"]
$ship_to["state_province_code"]
$ship_to["postal_code"]
The Address Validation request must contain one of the following input combinations:
¥ City, State, and Postal Code
¥ City
¥ Postal Code
¥ City and State
¥ City and Postal Code
¥ State and Postal Code
*/

// define some required values
$access_license_number = $CFG->ups_xml_access_key;
$user_id = $CFG->ups_userid;
$password       = $CFG->ups_password;
if ($CFG->ups_testmode == "FALSE") {
    $post_url = "https://www.ups.com/ups.app/xml/AV";
} else {
    $post_url = "https://wwwcie.ups.com/ups.app/xml/AV";
}

// construct the xml query document
$xml_request = "<?xml version=\"1.0\"?>
<AccessRequest>
<AccessLicenseNumber>
      $access_license_number
</AccessLicenseNumber>
<UserId>
      $user_id
</UserId>
<Password>
      $password
</Password>
</AccessRequest>
<?xml version=\"1.0\"?>
<AddressValidationRequest xml:lang=\"en-US\">
<Request>
<TransactionReference>
<CustomerContext>
Maryam Dennis-Customer Data
</CustomerContext>
<XpciVersion>
1.0001
</XpciVersion>
</TransactionReference>
<RequestAction>AV</RequestAction>
</Request>
<Address>
<City>$ship_to[city]</City>
<StateProvinceCode>$ship_to[state_province_code]</StateProvinceCode>
<PostalCode>$ship_to[postal_code]</PostalCode>
</Address>
</AddressValidationRequest>";

// execute the curl function and return the result document to $result
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL,$post_url);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml_request");
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$xml_result = curl_exec ($ch);
curl_close ($ch);

$data = parse_xml($xml_result);
$result = array();

if ($data["AddressValidationResponse"]["#"]["Response"][0]["#"]["ResponseStatusCode"][0]["#"] == 1) {

    $result["response_status_code"] = 1;
    $end = 0;
    $count = 0;
    while ($end == 0) {
        if (isset($data["AddressValidationResponse"]["#"]["AddressValidationResult"][$count]["#"]["Rank"][0]["#"])) {
       
            $result[$count]["rank"] = $data["AddressValidationResponse"]["#"]["AddressValidationResult"][$count]["#"]["Rank"][0]["#"];
       
            $result[$count]["city"] = $data["AddressValidationResponse"]["#"]["AddressValidationResult"][$count]["#"]["Address"][0]["#"]["City"][0]["#"];
       
            $result[$count]["state"] = $data["AddressValidationResponse"]["#"]["AddressValidationResult"][$count]["#"]["Address"][0]["#"]["StateProvinceCode"][0]["#"];

            $result[$count]["postal_low_end"] = $data["AddressValidationResponse"]["#"]["AddressValidationResult"][$count]["#"]["PostalCodeLowEnd"][0]["#"];

            $result[$count]["postal_high_end"] = $data["AddressValidationResponse"]["#"]["AddressValidationResult"][$count]["#"]["PostalCodeHighEnd"][0]["#"];

            $result["count"] = $count;

    } else {
        $end = 1;
    }
    $count++;
    } //end while
} else {

$result["response_status_code"]      = $data["AddressValidationResponse"]["#"]["Response"][0]["#"]["ResponseStatusCode"][0]["#"];

$result["error_description"] = $data["AddressValidationResponse"]["#"]["Response"][0]["#"]["Error"][0]["#"]["ErrorDescription"][0]["#"];

}
0
kerkor
Asked:
kerkor
1 Solution
 
Marcus BointonCommented:
Instead of falling all the way back to fsockopen, you could instead try using PEAR's HTTP_Client and HTTP_Request classes which do similar things to CURL, but using only native PHP functions, no extension required.
http://pear.php.net/package/HTTP_Client
http://pear.php.net/package/HTTP_Request

Your XML construction and parsing doesn't need changing, so all you need to replace is the CURL part, which could be something like:

require_once 'HTTP/Request.php';
$req =& new HTTP_Request('');
$req->setMethod(HTTP_REQUEST_METHOD_POST);
$req->setURL($post_url);
$req->addPostData('xml', $xml_request);
$req->sendRequest();
$data = $req->getResponseBody();

This is untested - the only bit I'm unsure of is what the field name should be for the XML data.
0
 
kerkorAuthor Commented:
I ended up installing cURL but thanks for the other avenue.

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now