Php 在Symfony2中使用OAuth 1类
您好,我想将Yahoo BOSS API与Symfony2集成,但是Yahoo建议的OAuth代码类似乎无法与现代PHP框架配合使用Php 在Symfony2中使用OAuth 1类,php,symfony,oauth,Php,Symfony,Oauth,您好,我想将Yahoo BOSS API与Symfony2集成,但是Yahoo建议的OAuth代码类似乎无法与现代PHP框架配合使用 我认为OAuth类存在名称空间问题,我需要采取什么步骤在Symfony2中实现这个类?在Symfony 2中,我可以推荐一个支持OAuth 1和2的第三方包 看看:在Symfony 2中,我可以推荐一个支持OAuth 1和2的第三方包 看看:1)创建一个类似于Project/src/OAuth的目录 2) 将类放在该目录中的单独文件中 3) 添加名称空间OAu
我认为OAuth类存在名称空间问题,我需要采取什么步骤在Symfony2中实现这个类?在Symfony 2中,我可以推荐一个支持OAuth 1和2的第三方包
看看:在Symfony 2中,我可以推荐一个支持OAuth 1和2的第三方包 看看:1)创建一个类似于Project/src/OAuth的目录 2) 将类放在该目录中的单独文件中 3) 添加
名称空间OAuth代码>在每个OAuth类的开头
4) 向异常类添加反斜杠(或添加使用异常;
):
5) 避免在类名(,)中使用下划线:
6) 修复OAuthUtil中的数组映射调用:
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
);
}
}
1)创建一个类似于Project/src/OAuth的目录
2) 将类放在该目录中的单独文件中
3) 添加名称空间OAuth代码>在每个OAuth类的开头
4) 向异常类添加反斜杠(或添加使用异常;
):
5) 避免在类名(,)中使用下划线:
6) 修复OAuthUtil中的数组映射调用:
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
);
}
}
您可以将其创建为一项服务-请参见Hi,我打算在它工作后将其实现为一项服务。谢谢您觉得我的答案有用吗?您可以将其创建为服务-您好,我打算在它工作后将其实现为服务。谢谢你觉得我的答案有用吗?嗨,我不需要添加一个bundle来访问OAuth 1的RESTAPI,那个项目有很多我不需要的功能。谢谢你!没有人会强迫您添加此捆绑包。但是你可以看看它是如何工作的,然后看看你需要什么。嗨,我不需要添加一个bundle来访问OAuth1的RESTAPI,这个项目有很多我不需要的功能。谢谢你!没有人会强迫您添加此捆绑包。但是你可以看看它是如何工作的,并得到你需要的。
<?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
);
}
}