kulor (owner)

Revisions

gist: 57536 Download_button fork
public
Public Clone URL: git://gist.github.com/57536.git
Embed All Files: show embed
yql_lib.php #
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
        
// include OAuth lib
require("OAuth.php");
 
class yql_lib{
    function __construct(){
        // define your consumer key
        $this->consumerKey = "";
 
        // define your consumer key secret
        $this->consumerSecret = "";
 
        // Don't have an app set up yet?
        // Sign up for one here:
        // https://developer.yahoo.com/dashboard/createKey.html
    }
    
    public function test_query(){
        // make sample request to YQL
        $data = $this->query("select name,centroid,woeid from geo.places where text=\"YVR\"");
        return $data;
    }
 
 
    public function query($query)
    {
        // define the base URL to the YQL web-service
        $base_url = "http://query.yahooapis.com/v1/yql";
 
        // create arguments to sign.
        $args = array();
        $args["q"] = $query;
        $args["format"] = "json"; // Nice and easy for us to parse
 
        // passing the key and secret strings to define our consumer.
        $consumer = new OAuthConsumer($this->consumerKey, $this->consumerSecret);
 
        // build and sign the request
        $request = OAuthRequest::from_consumer_and_token($consumer, NULL, "GET", $base_url, $args);
        $request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer, NULL);
 
        // finally create the URL
        $url = sprintf("%s?%s", $base_url, $this->oauth_http_build_query($args));
 
        // and the OAuth Authorization header
        // $headers = array($request->to_header()); // Default does not contain the realm. But we need it
        $headers = array($request->to_header('yahooapis.com'));
 
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $rsp = curl_exec($ch);
 
        $data = json_decode($rsp);
 
        // since we requested JSON we'll decode it
        // and return the data as a PHP object
        if(isset($data->query)){
            // print the result.
            $results = $data->query->results;
            return $results;
        } else {
            return false;
        }
    }
 
 
    private function oauth_http_build_query($parameters) {
        $strings = array();
        foreach($parameters as $name => $value) {
            // Convert a url key=value to array values
            $strings[] = sprintf("%s=%s", rawurlencode($name), rawurlencode($value));
        }
        $query = implode("&", $strings);
        return $query;
    }
}