【问题标题】:How to send a patch api request using a variable如何使用变量发送补丁 api 请求
【发布时间】:2019-07-19 00:55:52
【问题描述】:

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

此代码适用于手动输入的 userId。 userId和bearer code是为了这个问题而编造的。

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。

因此->

补丁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

【问题讨论】:

    标签: api patch guzzle userid zoom-sdk


    【解决方案1】:

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

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

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

    您在第一个参数中添加 URL 路径,因为路径是 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
    ]);
    

    【讨论】:

    • 谢谢 ncla,我会测试你的解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-21
    • 2015-09-15
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多