1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
<?php # searchmash-proxy.php // Adapted from: // PHP Proxy example for Yahoo! Web services. // Responds to both HTTP GET and POST requests (only GET for this one). // Author: Jason Levitt // December 7th, 2005 // $url = 'http://www.searchmash.com/results/%query%+site:sujitpal.blogspot.com'; // Get the REST GET call from the AJAX application $qt = $_GET['qt']; $url = str_replace("%query%", $qt, $url); // Open the Curl session $session = curl_init($url); // Don't return HTTP headers. Do return the contents of the call curl_setopt($session, CURLOPT_HEADER, false); curl_setopt($session, CURLOPT_RETURNTRANSFER, true); // Make the call $results = curl_exec($session); // The web service returns JSON. Set the Content-Type appropriately header("Content-Type: application/json"); echo $results; curl_close($session); ?>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
I copy both these files to the document root of my Lighttpd server and navigate to the HTML file (http://localhost:81/search-blog.html) on my browser, then enter the term in the search box and hit the 'Search!' button. Search results for the term 'json' are shown below:
There are several things I liked about this approach. First, no more futzing with browser detection and using XmlHttpRequest or its Microsoft cousin XMLHTTP directly. Second, the use of nested anonymous functions that improves the readability of the code. And third, the use of nested JSON objects to pass arguments to the function.