1

やあ、Yahoo BOSS API を Symfony2 に統合したいのですが、Yahoo が提案する 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

OAuth クラスには名前空間の問題があると思いますが、Symfony2 でこのクラスを実装するにはどのような手順が必要ですか?

4

2 に答える 2

5

1) Project/src/ OAuthのようなディレクトリを作成します

2) クラスをそのディレクトリ内の個別のファイルに配置します。

namespace OAuth;3)すべての OAuth クラスの先頭に追加します。

4) 例外クラスにバックスラッシュを追加 (または追加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

于 2013-10-21T22:09:49.403 に答える
4

Symfony 2 では、OAuth 1 と 2 をサポートするサードパーティ バンドルをお勧めします。

見てみましょう: https://github.com/hwi/HWIOAuthBundle

于 2013-09-16T16:58:08.927 に答える