1

我正在尝试访问 wordpress.com API,但在使用 Guzzle 时遇到了问题。

$client = new GuzzleHttp\Client();
$request = $client->post('https://public-api.wordpress.com/oauth2/token')
        ->setPostField('client_id', Config::get('wordpress.id'))
        ->setPostField('redirect_uri', Config::get('wordpress.redirect_url'))
        ->setPostField('client_secret', Config::get('wordpress.secret'))
        ->setPostField( 'code', $code)
        ->setPostField('grant_type', 'authorization_code');

如果我将所有数据输入 Postman,它就可以工作!但是,如果我使用 guzzle,端点会以 400 响应。这让我相信帖子数据没有被发送,但刚刚开始使用 guzzle 我不知道为什么。我已经检查了所有的Config::get...回报,他们应该这样做$code

关于我应该做什么的任何想法?

更新 1

这适用于 cURL:

    $curl = curl_init( "https://public-api.wordpress.com/oauth2/token" );
    curl_setopt( $curl, CURLOPT_POST, true );
    curl_setopt( $curl, CURLOPT_POSTFIELDS, array(
        'client_id' => Config::get('wordpress.id'),
        'redirect_uri' => Config::get('wordpress.redirect_url'),
        'client_secret' => Config::get('wordpress.secret'),
        'code' => $code, // The code from the previous request
        'grant_type' => 'authorization_code'
    ) );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1);
    $auth = curl_exec( $curl );
    $secret = json_decode($auth);
    $access_key = $secret->access_token;
    dd($access_key);
4

1 回答 1

2

还有其他人遇到与您在此处描述的类似的问题。看起来很灵活,数组方法可能是一次添加这些值对的好方法?

根据Guzzle 文档,似乎还有另一种格式选项也可以像这样声明它:

$request = $client->post('http://httpbin.org/post', array(), array(
    'custom_field' => 'my custom value',
    'file_field'   => '@/path/to/file.xml'
));
$response = $request->send();

有什么可以尝试的。

于 2014-06-05T20:32:52.977 回答