【问题标题】:Posting data using Guzzle使用 Guzzle 发布数据
【发布时间】:2019-02-23 11:11:29
【问题描述】:

我正在尝试使用 Guzzle 6 发布数据,

我动态生成的请求如下所示:

$postRequest = Array(
    'headers' => array(
        ['x-api-key'] => 'srDxd39M2FQxxvfvxxcIohcLfKDcdcRUU'
    )
    'form_params' => array(
            [0] => Array ( 
                ['name'] => 'function_key' 
                ['contents'] => 'REGISTER' 
            ) 
            [1] => Array ( 
                ['name'] => 'email' 
                ['contents'] => 'tester@test.com' 
            ) 
            [2] => Array ( 
                ['name'] => 'password' 
                ['contents'] => 'test' 
            ) 
            [3] => Array ( 
                ['name'] => 'name' 
                ['contents'] => 'tester' 
            ) 
            [4] => Array ( 
                ['name'] => 'is_org' 
                ['contents'] => 'N' 
            ) 
        )
)
// Sending Request using 'POST' Method                        
$client = new GuzzleHttp\Client();
$response = $client->request('POST','abcdxyz.com',$getRequest);  

现在我的问题是,我在发送上述请求后收到的响应显示“API 'function_key' is missing”。 但是我将“function_key”作为请求的一部分发送,那么我在这里缺少什么?为什么找不到'function_key'

感谢任何帮助

TIA

【问题讨论】:

  • 你的 x-api-key 和 function_key 一样吗?
  • 两者都不同

标签: php guzzle guzzle6


【解决方案1】:

尝试如下格式化:

$postRequest = [
    'headers' => [
        'x-api-key' => 'srDxd39M2FQxxvfvxxcIohcLfKDcdcRUU'
    ],
    'form_params' => [
        'function_key' => 'REGISTER',
        'email' => 'tester@test.com', 
        'password' => 'test', 
        'name' => 'tester', 
        'is_org' => 'N' 
     ]   
]

【讨论】:

  • 他的数组是 print_r 的输出,但它的格式与我的示例不同。
  • @DraskoR 我试过像你的例子那样格式化,但还是同样的问题
  • @VishalVarshney 你也在创建一个 $postRequest 变量,但是在 $response = $client->request('POST','abcdxyz.com',$getRequest);我猜你在你的答案中打错了它,但只是想评论一下,以防你也把它留在你的代码中。在不知道您使用的是哪种 API 以及预期的格式的情况下,我认为除了猜测之外我无能为力。
猜你喜欢
  • 1970-01-01
  • 2015-08-25
  • 2017-05-17
  • 2020-04-12
  • 2020-03-19
  • 2021-12-26
  • 1970-01-01
  • 2019-12-08
  • 1970-01-01
相关资源
最近更新 更多