Sensis Developer Centre - SAPI Forums

API Forum

RSS Feed

PHP example with Curl

  1. If you have problems installing PECL, here is an example using Curl

    <?php

    function search($query, $location, $rows) {

    # put your API key here
    $apikey = "...";

    # location of the search API endpoint
    $endpoint = "http://api.sensis.com.au/20110229/search";

    # construct a URL with the query string, escaping any special characters.
    $url = $endpoint . "?key=" . $apikey .
    "&query=" . urlencode($query) .
    "&location=" . urlencode($location) .
    "&rows=" . urlencode($rows);

    //Construct the REST call /$url = 'http://localhost/Receiver1.php?file=' . $filename;
    //GET request with 'curl'
    $ch = curl_init($url);
    //Set Curl options
    curl_setopt ( $ch, CURLOPT_URL, $url );
    curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt ( $ch, CURLOPT_HEADER, false );

    //Make the request
    $response = curl_exec($ch);
    //Get the status codes
    $code = curl_getinfo($ch,CURLINFO_HTTP_CODE);

    //Close the curl session
    curl_close($ch);

    # convert the response message to an associative array
    $result = json_decode($response,true);

    # ensure successful status code
    if ($code == 200) { # success
    return $result;
    } else if ($code == 206) { # spell-checker was run
    echo "Note: " . $result["message"] . "\n";
    return result;
    } else {
    throw new Exception("API returned error: " .
    $result["message"] . ", code: " . $result["code"]);
    }

    }

    function example() {

    try {

    # perform a search for 'hairdresser'
    $results = search("hairdresser", "st kilda, vic",30);
    echo "Total results found: " . $results["totalResults"] . "\n";

    # the results member is an array containing each listings as a nested associative array
    foreach ($results["results"] as &$result) {
    echo $result["name"] . " (" . $result["primaryAddress"]["addressLine"] . ")\n";
    }

    } catch (Exception $e) {

    # something bad happened
    echo "Internal error: $e\n";

    }

    }

    example();


    ?>

    Message edited by Robert Parsons 5 years ago

    Tags

  2. dalehurley7 years ago

    Why would you use cURL? file_get_content using a query string is much easier.

    [code]
    $apikey = "...";
    $endpoint = "...";
    $url = $endpoint . "?key=" . $apikey .
    "&query=" . urlencode($query) .
    "&location=" . urlencode($location) .
    "&rows=" . urlencode($rows);
    $results=json_decode(file_get_content($url));
    [/code]

  3. Robert Parsons6 years ago

    Hi Dale,

    Thanks for your feedback. We were thinking of changing the PHP example in the documentation to use cURL since it's easier to install (as opposed to the HttpRequest object) and seems to be already available on most systems, but file_get_content() function looks much easier! The only drawback I can see is that it might be harder to detect certain HTTP errors, but then again I guess all you'd care about is if it worked or not - to find the actual cause you'd just have to look in the logs.

    Cheers,
    Rob.

  4. Sheetal5 years ago

    why the whole result is not shown, it only shows 20 pages and gives the message "Number of pages capped at 20, too many results found"

[ Page 1 of 1 ]