[英]Use OAuth 1 Class in Symfony2
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. 嗨,我想将Yahoo BOSS API与Symfony2集成,但是Yahoo建议的OAuth代码类似乎不适用于现代PHP框架。
http://oauth.googlecode.com/svn/code/php/OAuth.php 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 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? 我认为OAuth类存在名称空间问题,在Symfony2中实现该类需要采取哪些步骤?
1) Create a directory like Project/src/ OAuth 1)创建一个目录,例如Project / src / OAuth
2) Place the classes in separate files inside that directory. 2)将类放在该目录内的单独文件中。
3) Add namespace OAuth;
3)添加
namespace OAuth;
at the beginning of every OAuth class. 在每个OAuth类的开头。
4) Add a backslash to the Exception class (or add use Exception;
): 4)在Exception类中添加一个反斜杠(或添加
use Exception;
):
class OAuthException extends \Exception
5) Avoid underscores in class names ( Laravel-Oauth2 Issue in Laravel 4 , Underscores in Namespaces and Class Names ): 5)避免在类名中使用下划线( Laravel 4中的Laravel-Oauth2问题 , 在名称空间和类名中使用下划线 ):
abstract class OAuthSignatureMethodRSASHA1 extends OAuthSignatureMethod
class OAuthSignatureMethodPLAINTEXT extends OAuthSignatureMethod
class OAuthSignatureMethodHMACSHA1 extends OAuthSignatureMethod
6) Fix the array_map call in OAuthUtil: 6)修复OAuthUtil中的array_map调用:
return array_map(array('OAuth\OAuthUtil', 'urlencode_rfc3986'), $input);
7) And finally use it: 7)最后使用它:
<?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
您可以从这里获取课程: https : //github.com/coma/OAuthSOSample
In Symfony 2 I can recommend a third party bundle that will support OAuth 1 & 2. 在Symfony 2中,我可以推荐一个支持OAuth 1和2的第三方捆绑软件。
Take at look: https://github.com/hwi/HWIOAuthBundle 看一下: https : //github.com/hwi/HWIOAuthBundle
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.