1)创建一个目录,如 Project/src/ OAuth
2) 将类放在该目录内的单独文件中。
3)namespace OAuth;
在每个 OAuth 类的开头添加。
4) 向 Exception 类添加反斜杠(或添加use Exception;
):
class OAuthException extends \Exception
5) 避免类名中的下划线(Laravel 4 中的 Laravel-Oauth2 问题,命名空间和类名中的下划线):
abstract class OAuthSignatureMethodRSASHA1 extends OAuthSignatureMethod
class OAuthSignatureMethodPLAINTEXT extends OAuthSignatureMethod
class OAuthSignatureMethodHMACSHA1 extends OAuthSignatureMethod
6) 修复 OAuthUtil 中的 array_map 调用:
return array_map(array('OAuth\OAuthUtil', 'urlencode_rfc3986'), $input);
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
);
}
}
所以这些是我遵循的步骤,它奏效了;你可以从这里获取课程:https ://github.com/coma/OAuthSOSample