【问题标题】:Guzzle post request working in browser but not in appGuzzle 发布请求在浏览器中工作但不在应用程序中
【发布时间】:2014-06-05 18:21:23
【问题描述】:

我正在尝试访问 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);

【问题讨论】:

    标签: php wordpress curl laravel guzzle


    【解决方案1】:

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

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

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

    尝试一下。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-05
      • 2013-01-17
      • 1970-01-01
      • 2020-05-18
      • 1970-01-01
      • 2019-10-02
      • 2014-08-21
      相关资源
      最近更新 更多