POST请求通过Guzzle php

时间:2017-07-15 09:40:55

标签: php json post request guzzle

嗨我想发一个帖子请求dropbox.i发现有这个guzzle,可以很容易地发送http req使用PHP。这里是dropbox提供的http代码

    `POST /2/files/list_folder
    Host: https://api.dropboxapi.com
    User-Agent: api-explorer-client
    Authorization: Bearer <access-token>
    Content-Type: application/json
    {
    "path": "",
    "recursive": false,
    "include_media_info": true,
    "include_deleted": false,
    "include_has_explicit_shared_members": false
    }`

这是我为上述请求编码的php代码;

$response=$client->request('POST','https://api.dropboxapi.com',
[
    'headers'=>[
        'Authorization'=>'Bearer '.$API,
        'User-Agen'=>'api-explorer-client',
        'Content-Type'=>'application/json'
    ],
    'form_params'=>[

            'path'=>'',
            'recursive'=>'false',
            'include_media_info'=> 'true',
            'include_deleted'=> 'false',
            'include_has_explicit_shared_members'=> 'false'

    ]
]);

$headers = $response->getHeaders();
$body =$response->getBody();
$print=json_decode($body,true);
//Output headers and body for debugging purposes
var_dump($headers);echo"<br><br>";
var_dump($print);?>

这应该给出类似下面的内容,但我得到一个小数组

    {"entries": [
{
".tag": "folder",
  "name": "Photos",
  "path_lower": "/photos",
  "path_display": "/Photos",
  "id": "id:bwGPfg_v6ZUAAAAAAAAAOA"
},`
我错过了什么? PS:我刚接触php的相关内容:)

1 个答案:

答案 0 :(得分:0)

您正在发送JSON,而不是HTML表单。因此,请使用'json'代替'form_params'