如何使用变量发送补丁API请求

时间:2019-07-19 00:55:52

标签: api zoom patch guzzle userid

我正在尝试使用他们的API更新Zoom Conference应用程序中的用户类型。我按照他们的文档使用PATCH,当我在URL中对userId进行硬编码时,此方法有效,但是我需要使用数组变量,因为多个用户需要一次更新。

此代码适用于手动输入的userId。 userId和承载代码是针对此问题的。

require 'vendor/autoload.php';
use GuzzleHttp\Client;
$client = new Client();

$response = $client->PATCH('https://api.zoom.us/v2/users/jkdflg4589jlmfdhw7', [

'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer my token goes here',
],

'body' => json_encode([
    'type' => '1',
])
]);

$body = $response->getBody() ;
$string = $body->getContents(); 
$json = json_decode($string);

通过这种方式,代码可以工作并将我的用户类型更改为1。

下面的代码是行不通的。 在Zoom API参考中,有一个测试部分,并且可以在“路径参数”字段下的“设置”标签中添加userId。

https://marketplace.zoom.us/docs/api-reference/zoom-api/users/userupdate

因此,我可以在其中添加userId,当我运行它时,它实际上将URL中的{userId}替换为url patch命令中的实际userId。

因此而来->

PATCH https://api.zoom.us/v2/users/ {userId}

经过所有转换,脚本, 然后运行变量替换。

匹配https://api.zoom.us/v2/users/jkdflg4589jlmfdhw7

但是,当我在代码中尝试使用它时,它不起作用,我不知道在何处添加路径参数。我更习惯于PHP,但是我将尽我所能使其工作。另外,我希望userId是一个可能包含1个或多个userIds(数组)的变量。

这是我的无效代码:

require 'vendor/autoload.php';
use GuzzleHttp\Client;
$client = new Client();

$response = $client->PATCH('https://api.zoom.us/v2/users/{userId}', [

'params' => [
'userId' => 'jkdflg4589jlmfdhw7',
],

'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer my token goes here',
],

'body' => json_encode([
    'type' => '1',
])
]);

$body = $response->getBody() ;
$string = $body->getContents(); 
$json = json_decode($string);

我的代码因错误而失败:

致命错误:未捕获的GuzzleHttp \ Exception \ ClientException:客户端错误:PATCH https://api.zoom.us/v2/users/%7BuserId%7D导致了404 Not Found响应:{“代码”:1001,“消息”:“用户不存在:{userId} “} 在/home/.../Zoom_API_V2/guzzle_response/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:113堆栈跟踪中:

0 /home/.../Zoom_API_V2/guzzle_response/vendor/guzzlehttp/guzzle/src/Middleware.php(66):GuzzleHttp \ Exception \ RequestException :: create(Object(GuzzleHttp \ Psr7 \ Request),Object( GuzzleHttp \ Psr7 \ Response))

1 /home/.../Zoom_API_V2/guzzle_response/vendor/guzzlehttp/promises/src/Promise.php(203):GuzzleHttp \ Middleware :: GuzzleHttp {closure}(Object(GuzzleHttp \ Psr7 \ Response))< / h1>

2 /home/.../Zoom_API_V2/guzzle_response/vendor/guzzlehttp/promises/src/Promise.php(156):GuzzleHttp \ Promise \ Promise :: callHandler(1,Object(GuzzleHttp \ Psr7 \ Response),数组)

3 /home/.../publ在/home/.../Zoom_API_V2/guzzle_response/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php,位于第113行

感谢您的协助。 克里斯

1 个答案:

答案 0 :(得分:0)

如果我对您的理解正确,那么这就是您要尝试的PHP中的基本字符串连接

$userId = 'jkdflg4589jlmfdhw7';

$response = $client->PATCH('https://api.zoom.us/v2/users/' . $userId, [
    // other options
]);
  

但是,当我在代码中尝试使用它时,它不起作用,我不知道在何处添加路径参数。

您将URL路径添加到第一个参数中,因为path是URL的一部分。但是,您可以通过Guzzle选项(而不是路径)设置查询参数(例如,用于GET请求)和表单数据(例如,用于POST表单请求)。

  

我也希望userId是一个可能包含1个或多个userId(数组)的变量。

仅使用简单的implode即可将数组转换为以逗号分隔的列表,但是链接到的API点似乎不支持多个用户ID。

$userId = ['jkdflg4589jlmfdhw7', 'asdfa123sdfasdf'];

$response = $client->PATCH('https://api.zoom.us/v2/users/' . implode(',', $userId), [
    // other options
]);
相关问题