api - 如何使用变量发送补丁 api 请求
问题描述
我正在尝试使用他们的 API 更新 Zoom 会议应用程序中的用户类型。我根据他们的文档使用 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 参考中有一个测试部分,用户 ID 可以添加到字段下的一个名为“设置”的选项卡中:路径参数。
https://marketplace.zoom.us/docs/api-reference/zoom-api/users/userupdate
因此我可以在那里添加 userId,当我运行它时,它实际上将 URL 中的 {userId} 替换为实际的 userId 到 url 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);
我的代码因错误而失败:
Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: `PATCH https://api.zoom.us/v2/users/%7BuserId%7D` resulted in a `404 Not Found` response: {"code":1001,"message":"User not exist: {userId}"}
in /home/.../Zoom_API_V2/guzzle_response/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:113 Stack trace:
#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))
#2 /home/.../Zoom_API_V2/guzzle_response/vendor/guzzlehttp/promises/src/Promise.php(156): GuzzleHttp\Promise\Promise::callHandler(1, Object(GuzzleHttp\Psr7\Response), Array)
#3 /home/.../publ in /home/.../Zoom_API_V2/guzzle_response/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php on line 113
解决方案
如果我理解正确,那么这是您尝试执行的 PHP 中的基本字符串连接
$userId = 'jkdflg4589jlmfdhw7';
$response = $client->PATCH('https://api.zoom.us/v2/users/' . $userId, [
// other options
]);
但是,当我在我的代码中尝试它时它不起作用,我不知道在哪里添加路径参数。
您在第一个参数中添加 URL 路径,因为路径是 URL 的一部分。但是,您可以通过 Guzzle 选项设置查询参数(例如对于 GET 请求)和表单数据(例如对于 POST 表单请求),但不能设置路径。
另外我希望 userId 是一个可能包含 1 个或多个 userIds (数组)的变量。
仅使用简单implode
的将数组转换为逗号分隔列表应该可以,但是您链接到的 API 点似乎不支持多个用户 ID。
$userId = ['jkdflg4589jlmfdhw7', 'asdfa123sdfasdf'];
$response = $client->PATCH('https://api.zoom.us/v2/users/' . implode(',', $userId), [
// other options
]);
推荐阅读
- javascript - 如何使用嵌套的 optgroup reactjs
- php - Wordpress:在管理仪表板中按计数对注册用户的帖子列进行排序?
- python - 如何通过 main.py 文件访问 ScreenManager 命令?
- c++ - 使用 std::regex 或 boost::regex 库匹配所有以“.txt”结尾的文件的正则表达式是什么?
- reactjs - 如何使用 jest 和酶检查单击后组件是否消失?
- javascript - 底页模态无法在 Expo 中运行
- python - 格式字符串的参数不足,请帮助 Python
- javascript - 为什么尝试更改 react js 中的 className 时 CSS 样式会消失?
- python-venv - Python- Venv - 访问 venv 之外的文件
- reactjs - nginx ssl + react + node,响应超时