Skip to content

Instantly share code, notes, and snippets.

@Ryuske
Created October 22, 2014 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ryuske/882651e7bde19afc8feb to your computer and use it in GitHub Desktop.
Save Ryuske/882651e7bde19afc8feb to your computer and use it in GitHub Desktop.
Laravel 4 Yahoo Contacts API Integration
<?php
/**
* Author: Kenyon Haliwell
* Date: 10/22/14
*
* Used to authenticate with Yahoo via oAuth2 and then use their Contacts API to return emails.
*
* Step 1: send user to Yahoo@getContactsLink
* Step 2: return user to a route that calls Yahoo@getContactsArray
* Step 3: Whatever you wanted to do with the contacts
*
* Note: If using Laravel 4 I put this under app/models/oauth2/Yahoo.php
* Note: You can use this with any framework/code, just follow these couple steps:
* Step 1: Remove namespace
* Step 2: Change \Auth::user()->id to logged in users ID
* Step 3: Change \Input::get('thing') to $_GET['thing']
* Step 4: Change Session::put/Session::get/Session::pull to $_SESSION (add key=>value, get value, get value and unset)
* Step 5: Use like a regular class.
*/
namespace App\Models\oAuth2;
class Yahoo {
static public $consumer_key = 'xxx';
static public $consumer_secret = 'xxx';
/**
* Get the oAuth2 link
*
* @return mixed
*/
static public function getContactsLink() {
parse_str(self::curl('https://api.login.yahoo.com/oauth/v2/get_request_token', true, [
'oauth_consumer_key' => self::$consumer_key,
'oauth_signature' => self::$consumer_secret . '&',
'oauth_signature_method' => 'PLAINTEXT',
'oauth_callback' => 'http://example.com/whatever-url-has-the-call-to-Yahoo@getContactsArray',
'oauth_nonce' => uniqid(rand()),
'oauth_timestamp' => time(),
'oauth_version' => '1.0',
'xoauth_lang_pref' => 'en-us'
]), $response);
\Session::put(\Auth::user()->id . '.oauth_token_secret', $response['oauth_token_secret']);
return $response['xoauth_request_auth_url'];
}
/**
* Get the oAuth2 access token
*
* @return mixed
*/
static public function getAccessToken() {
parse_str(self::curl(
'https://api.login.yahoo.com/oauth/v2/get_token?oauth_consumer_key=' . self::$consumer_key .'&oauth_signature=' . self::$consumer_secret . '%26' . \Session::pull(\Auth::user()->id . '.oauth_token_secret') . '&oauth_signature_method=PLAINTEXT&oauth_nonce=' . uniqid(rand()) . '&oauth_timestamp=' . time() . '&oauth_version=1.0&oauth_verifier=' . \Input::get('oauth_verifier') . '&oauth_token=' . \Input::get('oauth_token')
), $response);
return $response;
}
/**
* Get the contacts and return them via array
*
* @return array
*/
static public function getContactsArray() {
$emails = [];
$response = self::getAccessToken();
$response = self::curl('https://social.yahooapis.com/v1/user/' . $response['xoauth_yahoo_guid'] . '/contacts?format=json&oauth_token=' . urldecode($response['oauth_token']) . '&oauth_consumer_key=' . self::$consumer_key .'&oauth_signature=' . self::$consumer_secret . '%26' . $response['oauth_token_secret'] . '&oauth_signature_method=PLAINTEXT&oauth_nonce=' . uniqid(rand()) . '&oauth_timestamp=' . time() . '&oauth_version=1.0');
$response = json_decode($response, true);
foreach($response['contacts']['contact'] as $contact) {
if (array_key_exists('fields', $contact) && array_key_exists(1, $contact['fields']) && 'email' == $contact['fields'][1]['type']) {
$emails[] = $contact['fields']['1']['value'];
}
}
return $emails;
}
/**
* Used to perform curl requests for API interaction
*
* @param $url
* @param bool $post
* @param null $data
* @return mixed
*/
static public function curl($url, $post = false, $data = null) {
$s = curl_init();
curl_setopt($s,CURLOPT_URL, $url);
curl_setopt($s,CURLOPT_RETURNTRANSFER, true);
curl_setopt($s,CURLOPT_HEADER, false);
if(true === $post) {
curl_setopt($s,CURLOPT_POST, true);
curl_setopt($s,CURLOPT_POSTFIELDS, $data);
}
$response = curl_exec($s);
curl_close($s);
return $response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment