WhitePages Web Developer Portal Forums

General

RSS Feed

Sample PHP XML code which works

  1. Here is some PHP code that works with the XML api. This may help those coding API calls to WP.

    Usage Notes:
    -------------------
    * This code uses the SimpleXML library that is installed by default starting with PHP version 5.1
    * If your PHP doesn't have it, you can install it easily yourself. Check the PHP Manual for "SimpleXML"
    * The WP XML spec uses a namespace called "wp" which makes SimpleXML tricky. There is a workaround in this code which handles it. See the line with a "children()" call in it. That does the magic.
    * This code includes a function call to fsockopen(), which allows a timeout value to prevent your webpage hanging on the WP server
    * If you don't mind potentially waiting on WP's server, a "simplexml_load_file('URL')" call would suffice instead of the custom function
    * Insert your own API key, otherwise the code should work if you have SimpleXML in your PHP. Check phpinfo() to see if you have it.
    * I suggest you patch the search URL straight into a browser and view the raw XML. From that, you can determine the subscripting needed.

    Code:
    --------------

    <?php

    function loadXML2($domain, $path, $timeout) {

    $fp = fsockopen($domain, 80, $errno, $errstr, $timeout);
    if($fp) {
    // make request
    $out = "GET $path HTTP/1.1\r\n";
    $out .= "Host: $domain\r\n";
    $out .= "Connection: Close\r\n\r\n";
    fwrite($fp, $out);

    // get response
    $resp = "";
    while (!feof($fp)) {
    $resp .= fgets($fp, 128);
    }
    fclose($fp);
    // check status is 200
    $status_regex = "/HTTP\/1\.\d\s(\d+)/";
    if(preg_match($status_regex, $resp, $matches) && $matches[1] == 200) {
    // load xml as object
    $parts = explode("\r\n\r\n", $resp);
    return $parts[1];
    // return $resp;
    }
    }
    return false;

    }

    header('Content-Type: text/html');

    $apidomain = "api.whitepages.com";
    $apikey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // Put your API key here
    // People Search
    //$apisearch = "find_person";
    //$apifname = "mike";
    //$apilname = "smith";
    //$apizip = "98101";
    //$apipath = "/$apisearch/1.0/?firstname=$apifname;lastname=$apilname;zip=$apizip;api_key=$apikey";

    // Phone Search
    $apisearch = "reverse_phone";
    $apiphone = "2125867000"; // The phone number to reverse-search -- New York Hilton -- many listings
    //$apiphone = "7755888200"; // The phone number to reverse-search -- UPS Store -- one listing
    $apipath = "/$apisearch/1.0/?phone=$apiphone;api_key=$apikey";
    $timeout = 5; // API request will time out in 5 seconds -- tune as needed

    $apiretstr = loadXML2 ($apidomain, $apipath, $timeout);
    if($apiretstr != false) {

    $xmlraw = simplexml_load_string($apiretstr);
    $namespaces = $xmlraw->getNameSpaces(true);
    $xml = $xmlraw->children($namespaces['wp']);
    echo "<BR><BR><H1>Parsed variables: </H1><BR>";
    // var_dump($xml);

    // Parse out variables by referring to the object array returned, subscripted by the names of the indented dimensions

    // There is only one row in these array entries.
    echo "<BR>";
    echo "Result Type: " . $xml->result[type] . " <BR>";
    echo "Linkexpires: " . $xml->meta->linkexpiration[0] . " <BR>";
    echo "This Search URL: <A HREF='" . $xml->meta->searchlinks->link[1] . "'>" . $xml->meta->searchlinks->link[1] . "</A><BR>";
    echo "Records Available: " . $xml->meta->recordrange[totalavailable] . " <BR>";

    // There may be multiple rows, hence need to iterate
    foreach ($xml->listings as $listings) {
    foreach ($listings->listing as $listing) {
    echo "<BR>";
    echo "Addr Type: " . $listing->listingmeta->type . " <BR>";
    echo "Longitude: " . $listing->geodata->longitude . "<BR>";
    echo "Latitude: " . $listing->geodata->latitude . "<BR>";
    echo "Phone Owner: " . $listing->displayname . "<BR>";
    echo "Address : " . $listing->address->house . " " . $listing->address->street . " <BR>";
    echo "City/State : " . $listing->address->city . ", " . $listing->address->state . $listing->address->zip . "<BR>";
    echo "<A HREF='" . $listing->listingmeta->moreinfolinks->link[1] . "'>";
    echo $listing->listingmeta->moreinfolinks->link[1][linktext] . "</A><BR>";
    }
    }

    } else {
    // api call failed.
    }

    ?>

    Message edited by Walt Brubaker 3 years ago

  2. fcmarketing53 years ago

    thanks worked great!

[ Page 1 of 1 ]