/** * Call the Yahoo Contact API * @param string $consumer_key obtained when you registered your app * @param string $consumer_secret obtained when you registered your app * @param string $guid obtained from getacctok * @param string $access_token obtained from getacctok * @param string $access_token_secret obtained from getacctok * @param bool $usePost use HTTP POST instead of GET * @param bool $passOAuthInHeader pass the OAuth credentials in HTTP header * @return response string with token or empty array on error */ function call_yql($consumer_key, $consumer_secret, $querynum, $access_token, $access_token_secret, $usePost = false, $passOAuthInHeader = true) { $retarr = array(); // return value $response = array(); if ($querynum == 1) { $url = 'http://query.yahooapis.com/v1/yql'; // Show my profile $params['q'] = 'select * from social.profile where guid=me'; } elseif ($querynum == 2) { $url = 'http://query.yahooapis.com/v1/yql'; // Find my friends $params['q'] = 'select * from social.connections where owner_guid=me'; } else { // Since this information is public, use the non oauth endpoint 'public' $url = 'http://query.yahooapis.com/v1/public/yql'; // Find all sushi restaurants in SF order by number of ratings desc $params['q'] = 'select Title,Address,Rating from local.search where query="sushi" and location="san francisco, ca"|sort(field="Rating.TotalRatings",descending="true")'; } $params['format'] = 'json'; $params['callback'] = 'cbfunc'; $params['oauth_version'] = '1.0'; $params['oauth_nonce'] = mt_rand(); $params['oauth_timestamp'] = time(); $params['oauth_consumer_key'] = $consumer_key; $params['oauth_token'] = $access_token; // compute hmac-sha1 signature and add it to the params list $params['oauth_signature_method'] = 'HMAC-SHA1'; $params['oauth_signature'] = oauth_compute_hmac_sig($usePost ? 'POST' : 'GET', $url, $params, $consumer_secret, $access_token_secret); // Pass OAuth credentials in a separate header or in the query string if ($passOAuthInHeader) { $query_parameter_string = oauth_http_build_query($params, true); $header = build_oauth_header($params, "yahooapis.com"); $headers[] = $header; } else { $query_parameter_string = oauth_http_build_query($params); } // POST or GET the request if ($usePost) { $request_url = $url; logit("call_yql:INFO:request_url:{$request_url}"); logit("call_yql:INFO:post_body:{$query_parameter_string}"); $headers[] = 'Content-Type: application/x-www-form-urlencoded'; $response = do_post($request_url, $query_parameter_string, 80, $headers); } else { $request_url = $url . ($query_parameter_string ? '?' . $query_parameter_string : ''); logit("call_yql:INFO:request_url:{$request_url}"); $response = do_get($request_url, 80, $headers); } // extract successful response if (!empty($response)) { list($info, $header, $body) = $response; if ($body) { logit("call_yql:INFO:response:"); print json_pretty_print($body); } $retarr = $response; } return $retarr; }
function json_encode_pretty_print($data) { if (PHP_VERSION_ID >= 50400) { return json_encode($data, JSON_PRETTY_PRINT); } else { return json_pretty_print(json_encode($data), false); } }
/** * Call the Yahoo Contact API * @param string $consumer_key obtained when you registered your app * @param string $consumer_secret obtained when you registered your app * @param string $guid obtained from getacctok * @param string $access_token obtained from getacctok * @param string $access_token_secret obtained from getacctok * @param bool $usePost use HTTP POST instead of GET * @param bool $passOAuthInHeader pass the OAuth credentials in HTTP header * @return response string with token or empty array on error */ function callcontact($consumer_key, $consumer_secret, $guid, $access_token, $access_token_secret, $usePost = false, $passOAuthInHeader = true, $_count) { $retarr = array(); // return value $response = array(); $url = 'http://social.yahooapis.com/v1/user/' . $guid . '/contacts?count=' . $_count; $params['format'] = 'xml'; $params['view'] = 'compact'; $params['oauth_version'] = '1.0'; $params['oauth_nonce'] = mt_rand(); $params['oauth_timestamp'] = time(); $params['oauth_consumer_key'] = $consumer_key; $params['oauth_token'] = $access_token; // compute hmac-sha1 signature and add it to the params list $params['oauth_signature_method'] = 'HMAC-SHA1'; //$params['oauth_signature'] = // oauth_compute_hmac_sig($usePost? 'POST' : 'GET', $url, $params, // $consumer_secret, $access_token_secret); echo ','; echo $params['oauth_nonce']; echo ','; echo $params['oauth_timestamp']; echo ','; echo oauth_compute_hmac_sig($usePost ? 'POST' : 'GET', $url, $params, $consumer_secret, $access_token_secret); exit(0); // Pass OAuth credentials in a separate header or in the query string if ($passOAuthInHeader) { $query_parameter_string = oauth_http_build_query($params, true); $header = build_oauth_header($params, "yahooapis.com"); $headers[] = $header; } else { $query_parameter_string = oauth_http_build_query($params); } // POST or GET the request if ($usePost && 0) { $request_url = $url; logit("callcontact:INFO:request_url:{$request_url}"); logit("callcontact:INFO:post_body:{$query_parameter_string}"); $headers[] = 'Content-Type: application/x-www-form-urlencoded'; $response = do_post($request_url, $query_parameter_string, 80, $headers); } else { $request_url = $url . ($query_parameter_string ? '?' . $query_parameter_string : ''); logit("callcontact:INFO:request_url:{$request_url}"); $response = do_get($request_url, 80, $headers); } // extract successful response if (!empty($response)) { list($info, $header, $body) = $response; if ($body) { logit("callcontact:INFO:response:"); print json_pretty_print($body); } $retarr = $response; } return $retarr; }
/** * Call twitter to post a tweet * @param string $consumer_key obtained when you registered your app * @param string $consumer_secret obtained when you registered your app * @param string $status_message * @param string $access_token obtained from get_request_token * @param string $access_token_secret obtained from get_request_token * @param bool $usePost use HTTP POST instead of GET * @param bool $passOAuthInHeader pass OAuth credentials in HTTP header * @return response string or empty array on error */ function post_tweet($consumer_key, $consumer_secret, $status_message, $access_token, $access_token_secret, $usePost = false, $passOAuthInHeader = true) { $retarr = array(); // return value $response = array(); //$url = 'http://api.twitter.com/1/statuses/update.json'; $url = 'http://api.twitter.com/1.1/friendships/incoming.json'; //$params['status'] = $status_message; $params['oauth_version'] = '1.0'; $params['oauth_nonce'] = mt_rand(); $params['oauth_timestamp'] = time(); $params['oauth_consumer_key'] = $consumer_key; $params['oauth_token'] = $access_token; // compute hmac-sha1 signature and add it to the params list $params['oauth_signature_method'] = 'HMAC-SHA1'; $params['oauth_signature'] = oauth_compute_hmac_sig($usePost ? 'POST' : 'GET', $url, $params, $consumer_secret, $access_token_secret); // Pass OAuth credentials in a separate header or in the query string if ($passOAuthInHeader) { $query_parameter_string = oauth_http_build_query($params, true); $header = build_oauth_header($params, "Twitter API"); $headers[] = $header; } else { $query_parameter_string = oauth_http_build_query($params); } // POST or GET the request if ($usePost) { $request_url = $url; logit("tweet:INFO:request_url:{$request_url}"); logit("tweet:INFO:post_body:{$query_parameter_string}"); $headers[] = 'Content-Type: application/x-www-form-urlencoded'; $response = do_post($request_url, $query_parameter_string, 80, $headers); } else { $request_url = $url . ($query_parameter_string ? '?' . $query_parameter_string : ''); logit("tweet:INFO:request_url:{$request_url}"); $response = do_get($request_url, 80, $headers); } // extract successful response if (!empty($response)) { list($info, $header, $body) = $response; if ($body) { logit("tweet:INFO:response:"); print json_pretty_print($body); } $retarr = $response; } return $retarr; }
/** * Call the Yahoo Contact API * @param string $consumer_key obtained when you registered your app * @param string $consumer_secret obtained when you registered your app * @param string $guid obtained from getacctok * @param string $access_token obtained from getacctok * @param string $access_token_secret obtained from getacctok * @param bool $usePost use HTTP POST instead of GET * @param bool $passOAuthInHeader pass the OAuth credentials in HTTP header * @return response string with token or empty array on error */ function postcontact($consumer_key, $consumer_secret, $guid, $access_token, $access_token_secret, $usePost = false, $passOAuthInHeader = true) { $retarr = array(); // return value $response = array(); $post_body = '{"contact":{"fields":[{"type":"name","value":{"givenName":"John","middleName":"","familyName":"Doe","prefix":"","suffix":"","givenNameSound":"","familyNameSound":""}},{"type":"email","value":"*****@*****.**"}]}}'; $url = 'http://social.yahooapis.com/v1/user/' . $guid . '/contacts'; $params['oauth_version'] = '1.0'; $params['oauth_nonce'] = mt_rand(); $params['oauth_timestamp'] = time(); $params['oauth_consumer_key'] = $consumer_key; $params['oauth_token'] = $access_token; // compute hmac-sha1 signature and add it to the params list $params['oauth_signature_method'] = 'HMAC-SHA1'; $params['oauth_signature'] = oauth_compute_hmac_sig($usePost ? 'POST' : 'GET', $url, $params, $consumer_secret, $access_token_secret); // Pass OAuth credentials in a separate header or in the query string if ($passOAuthInHeader) { $query_parameter_string = oauth_http_build_query($params, true); $header = build_oauth_header($params, "yahooapis.com"); $headers[] = $header; $request_url = $url; } else { $query_parameter_string = oauth_http_build_query($params); $request_url = $url . '?' . $query_parameter_string; } // POST or GET the request if ($usePost) { logit("postcontact:INFO:request_url:{$request_url}"); logit("postcontact:INFO:post_body:{$post_body}"); $headers[] = 'Content-Type: application/json'; $response = do_post($request_url, $post_body, 80, $headers); } else { logit("postcontact:INFO:request_url:{$request_url}"); $response = do_get($request_url, 80, $headers); } // extract successful response if (!empty($response)) { list($info, $header, $body) = $response; if ($body) { logit("postcontact:INFO:response:"); print json_pretty_print($body); } $retarr = $response; } return $retarr; }
public function yahoo() { require_once "yahooapisrc/globals.php"; require_once "yahooapisrc/oauth_helper.php"; $request_token = $_GET['oauth_token']; $request_token_secret = $_SESSION['oauth_token_secret']; $oauth_verifier = $_GET['oauth_verifier']; // Get the access token using HTTP GET and HMAC-SHA1 signature $retarr = $this->get_access_token(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, $request_token, $request_token_secret, $oauth_verifier, false, true, true); if (!empty($retarr)) { list($info, $headers, $body, $body_parsed) = $retarr; if ($info['http_code'] == 200 && !empty($body)) { //print "Use oauth_token as the token for all of your API calls:\n" . rfc3986_decode($body_parsed['oauth_token']) . "\n"; } } // Fill in the next 3 variables. $guid = $body_parsed['xoauth_yahoo_guid']; $access_token = rfc3986_decode($body_parsed['oauth_token']); $access_token_secret = $body_parsed['oauth_token_secret']; // Call Contact API $retarr2 = $this->callcontact(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, $guid, $access_token, $access_token_secret, false, true); // extract successful response if (!empty($retarr2)) { list($info, $header, $body) = $retarr2; if ($body) { $json = json_pretty_print($body); $array_contacts = json_decode($json, TRUE); } } $return_contacts = array(); foreach ($array_contacts['contacts']['contact'] as $yahoo_contact) { foreach ($yahoo_contact['fields'] as $field) { if ($field['type'] == 'name') { $return_contacts[$yahoo_contact['id']]['name'] = $field['value']['givenName'] . " " . $field['value']['familyName']; } if ($field['type'] == 'yahooid') { $return_contacts[$yahoo_contact['id']]['email'] = $field['value'] . "@yahoo.com"; } elseif ($field['type'] == 'otherid') { $return_contacts[$yahoo_contact['id']]['email'] = $field['value']; } } } return $return_contacts; }