Hi I want to integrate the Yahoo BOSS API with Symfony2, but the OAuth code class suggested by Yahoo doesn't seem to work with modern PHP frameworks.
http://oauth.googlecode.com/svn/code/php/OAuth.php
/* Generic exception class
*/
class OAuthException extends Exception {
// pass
}
class OAuthConsumer {
public $key;
public $secret;
function __construct($key, $secret, $callback_url=NULL) {
$this->key = $key;
$this->secret = $secret;
$this->callback_url = $callback_url;
}
function __toString() {
return "OAuthConsumer[key=$this->key,secret=$this->secret]";
}
} [...]
http://developer.yahoo.com/boss/search/boss_api_guide/codeexamples.html#oauth_php
I think the OAuth class has namespace issues, what steps do I need to take to implement this class in Symfony2?
1) Create a directory like Project/src/ OAuth
2) Place the classes in separate files inside that directory.
3) Add namespace OAuth;
at the beginning of every OAuth class.
4) Add a backslash to the Exception class (or add use Exception;
):
class OAuthException extends \Exception
5) Avoid underscores in class names ( Laravel-Oauth2 Issue in Laravel 4 , Underscores in Namespaces and Class Names ):
abstract class OAuthSignatureMethodRSASHA1 extends OAuthSignatureMethod
class OAuthSignatureMethodPLAINTEXT extends OAuthSignatureMethod
class OAuthSignatureMethodHMACSHA1 extends OAuthSignatureMethod
6) Fix the array_map call in OAuthUtil:
return array_map(array('OAuth\OAuthUtil', 'urlencode_rfc3986'), $input);
7) And finally use it:
<?php
namespace My\PlaygroundBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use OAuth\OAuthConsumer;
use OAuth\OAuthRequest;
use OAuth\OAuthSignatureMethodHMACSHA1;
use OAuth\OAuthUtil;
class DefaultController extends Controller
{
/**
* @Route("/")
* @Template()
*/
public function indexAction()
{
$cc_key = "your consumer key here";
$cc_secret = "your consumer secret here";
$url = "http://yboss.yahooapis.com/ysearch/news,web,images";
$args = array();
$args["q"] = "yahoo";
$args["format"] = "json";
$consumer = new OAuthConsumer($cc_key, $cc_secret);
$request = OAuthRequest::from_consumer_and_token($consumer, NULL,"GET", $url, $args);
$request->sign_request(new OAuthSignatureMethodHMACSHA1(), $consumer, NULL);
$url = sprintf("%s?%s", $url, OAuthUtil::build_http_query($args));
$ch = curl_init();
$headers = array($request->to_header());
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$rsp = curl_exec($ch);
$results = json_decode($rsp);
return array(
'results' => $results
);
}
}
So those are the steps I followed and it worked; you can grab the classes from here: https://github.com/coma/OAuthSOSample
In Symfony 2 I can recommend a third party bundle that will support OAuth 1 & 2.
Take at look: https://github.com/hwi/HWIOAuthBundle
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.