• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 371
  • 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!!!


** 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
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\"?>
<?xml version=\"1.0\"?>
<AddressValidationRequest xml:lang=\"en-US\">
Maryam Dennis-Customer Data

// 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;
    } //end while
} else {

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

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

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.

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->addPostData('xml', $xml_request);
$data = $req->getResponseBody();

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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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