Translate cURL to fsockopen

Posted on 2005-04-18
Last Modified: 2012-06-27
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 = "";
} else {
    $post_url = "";

// 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]["#"];

Question by:kerkor
    LVL 25

    Accepted Solution

    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.

    Author Comment

    I ended up installing cURL but thanks for the other avenue.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    A colleague recently asked me about how to give his client a small part of the web site that could be completely under the client's control.  Since I have done this sort of thing before to add emergency banners to a web site, I decided I would creat…
    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now