dpfwhb7470 2014-06-05 18:21
浏览 153
已采纳

Guzzle发布请求在浏览器中工作但不在应用程序中

I am trying to access the wordpress.com API and am having trouble with 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');

If I input all the data into Postman it works! However, the endpoint responds with 400 if I use guzzle. This leads me to believe that the post data is not being sent but having only just started using guzzle I have no idea why. I have checked and all the Config::get... return what they should and so does $code.

Any ideas on what I should be doing?

Update 1

This works with 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);
  • 写回答

1条回答 默认 最新

  • dtczp02204 2014-06-05 20:32
    关注

    There is someone else having a similar problem to what you describe here. It looks lithe the array method might be a good solution to add those value pairs one at a time?

    There seems to be another format option according to the Guzzle docs that might work also declaring it like this:

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

    Something to try.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决