POST 使用 Guzzle 将数据更新到 EventBrite API
POST update data to EventBrite API using Guzzle
我可以使用 post 添加事件,代码如下:
//Create client
$client = new Client([
'base_uri' => 'https://www.eventbriteapi.com/v3/',
]);
$params = [
'event.name.html' => $this->Name,
'event.description.html' => $this->Description,
'event.listed' => false,
'event.start.utc' => $this->StartTime,
'event.start.timezone' => 'Europe/London',
'event.end.utc' => $this->EndTime,
'event.end.timezone' => 'Europe/London',
'event.currency' => 'GBP'
];
try {
//Send new event request
$res = $client->request('POST', "events/", [
'query' => ['token' => env('EVENTBRITE_TOKEN', '')],
'form_params' => $params
]);
} catch (GuzzleException $e) {
return false;
}
然而,当我尝试使用在第一步中创建的 ID 和以下代码更新事件时:
//Send update event request
$res = $client->request('POST', "events/$this->EventBriteID", [
'query' => ['token' => env('EVENTBRITE_TOKEN', '')],
'form_params' => $params
]);
它不更新事件。请求看起来成功了。状态 200 与事件对象一起返回很长时间,但是 none 的数据已更新。信息与创建活动时提交的信息保持一致。我做错了什么,我发现很难调试,因为端点返回 200 状态。
请求在 Postman 中运行良好,所以我在 Guzzle 中显然做错了什么。
编辑
作为 $response->request() 的请求输出
Client {#289 ▼
-config: array:8 [▼
"base_uri" => Uri {#298 ▼
-scheme: "https"
-userInfo: ""
-host: "www.eventbriteapi.com"
-port: null
-path: "/v3/"
-query: ""
-fragment: ""
}
"handler" => HandlerStack {#165 ▼
-handler: StreamHandler {#292 ▼
-lastHeaders: []
}
-stack: array:4 [▼
0 => array:2 [▼
0 => Closure {#293 ▼
class: "GuzzleHttp\Middleware"
parameters: {▼
$handler: {▼
typeHint: "callable"
}
}
file: "/home/user/mysite/vendor/guzzlehttp/guzzle/src/Middleware.php"
line: "54 to 69"
}
1 => "http_errors"
]
1 => array:2 [▼
0 => Closure {#296 ▼
class: "GuzzleHttp\Middleware"
parameters: {▼
$handler: {▼
typeHint: "callable"
}
}
file: "/home/user/mysite/vendor/guzzlehttp/guzzle/src/Middleware.php"
line: "148 to 150"
}
1 => "allow_redirects"
]
2 => array:2 [▼
0 => Closure {#295 ▼
class: "GuzzleHttp\Middleware"
parameters: {▼
$handler: {▼
typeHint: "callable"
}
}
file: "/home/user/mysite/vendor/guzzlehttp/guzzle/src/Middleware.php"
line: "27 to 43"
}
1 => "cookies"
]
3 => array:2 [▼
0 => Closure {#286 ▼
class: "GuzzleHttp\Middleware"
parameters: {▼
$handler: {▼
typeHint: "callable"
}
}
file: "/home/user/mysite/vendor/guzzlehttp/guzzle/src/Middleware.php"
line: "216 to 218"
}
1 => "prepare_body"
]
]
-cached: null
}
"allow_redirects" => array:5 [▼
"max" => 5
"protocols" => array:2 [▼
0 => "http"
1 => "https"
]
"strict" => false
"referer" => false
"track_redirects" => false
]
"http_errors" => true
"decode_content" => true
"verify" => true
"cookies" => false
"headers" => array:1 [▼
"User-Agent" => "GuzzleHttp/6.2.1 PHP/7.0.8-0ubuntu0.16.04.3"
]
]
}
测试您的令牌和参数的一个好方法是导航到 RapidAPI here. 我已将您直接链接到 EventbriteAPI updateEvent 端点。您应该继续将参数和您的令牌填写到表格中,然后单击测试。您应该会看到详细的 JSON 回复。这是一个简单的例子:
如果一切正常,请单击仪表板中响应正上方的代码,注册,从下拉列表中选择 PHP,RapidAPI 将提供 php 代码片段,您只需将其复制并直接粘贴到您的代码中即可进行 API 调用。下面是 RapidAPI 将提供的 PHP 代码片段示例:
require __DIR__ . '/vendor/autoload.php';
use RapidApi\rapidApiConnect;
$rapid = new RapidApiConnect("WhosebugTest", "##########################");
$result = $rapid->call('EventbriteAPI', 'updateEvent', [
'token' => '####################',
'eventId' => '31082267900',
'eventName' => 'Ghost Tour Test',
'eventStartUtc' => '2017-02-20T03:00:00Z',
'eventStartTimezone' => 'America/Los_Angeles',
'eventEndUtc' => '2017-02-20T06:00:00Z',
'eventEndTimezone' => 'America/Los_Angeles',
'eventCurrency' => 'USD',
'eventDescription' => ''
]);
希望对您有所帮助!
您很可能应该为 Guzzle 使用 body
请求选项而不是 form_params
。对请求使用 form_params
将发送 application/x-www-form-urlencoded
数据,这通常是 而不是 现代 API 所期望的。对于 Eventbrite,我相信它需要一个常规的 form-data
编码请求。
将您的 Guzzle 请求更改为:
$res = $client->request('POST', "events/$this->EventBriteID", [
'query' => ['token' => env('EVENTBRITE_TOKEN', '')],
'body' => $params
]);
希望这有效!
所以经过几个小时的绝望,我终于修好了。问题是端点 url:
上缺少尾部斜线
$res = $client->request('POST', "events/$this->EventBriteID", []);
改为
$res = $client->request('POST', "events/$this->EventBriteID/", []);
现在可以正常工作了。
我可以使用 post 添加事件,代码如下:
//Create client
$client = new Client([
'base_uri' => 'https://www.eventbriteapi.com/v3/',
]);
$params = [
'event.name.html' => $this->Name,
'event.description.html' => $this->Description,
'event.listed' => false,
'event.start.utc' => $this->StartTime,
'event.start.timezone' => 'Europe/London',
'event.end.utc' => $this->EndTime,
'event.end.timezone' => 'Europe/London',
'event.currency' => 'GBP'
];
try {
//Send new event request
$res = $client->request('POST', "events/", [
'query' => ['token' => env('EVENTBRITE_TOKEN', '')],
'form_params' => $params
]);
} catch (GuzzleException $e) {
return false;
}
然而,当我尝试使用在第一步中创建的 ID 和以下代码更新事件时:
//Send update event request
$res = $client->request('POST', "events/$this->EventBriteID", [
'query' => ['token' => env('EVENTBRITE_TOKEN', '')],
'form_params' => $params
]);
它不更新事件。请求看起来成功了。状态 200 与事件对象一起返回很长时间,但是 none 的数据已更新。信息与创建活动时提交的信息保持一致。我做错了什么,我发现很难调试,因为端点返回 200 状态。
请求在 Postman 中运行良好,所以我在 Guzzle 中显然做错了什么。
编辑
作为 $response->request() 的请求输出
Client {#289 ▼
-config: array:8 [▼
"base_uri" => Uri {#298 ▼
-scheme: "https"
-userInfo: ""
-host: "www.eventbriteapi.com"
-port: null
-path: "/v3/"
-query: ""
-fragment: ""
}
"handler" => HandlerStack {#165 ▼
-handler: StreamHandler {#292 ▼
-lastHeaders: []
}
-stack: array:4 [▼
0 => array:2 [▼
0 => Closure {#293 ▼
class: "GuzzleHttp\Middleware"
parameters: {▼
$handler: {▼
typeHint: "callable"
}
}
file: "/home/user/mysite/vendor/guzzlehttp/guzzle/src/Middleware.php"
line: "54 to 69"
}
1 => "http_errors"
]
1 => array:2 [▼
0 => Closure {#296 ▼
class: "GuzzleHttp\Middleware"
parameters: {▼
$handler: {▼
typeHint: "callable"
}
}
file: "/home/user/mysite/vendor/guzzlehttp/guzzle/src/Middleware.php"
line: "148 to 150"
}
1 => "allow_redirects"
]
2 => array:2 [▼
0 => Closure {#295 ▼
class: "GuzzleHttp\Middleware"
parameters: {▼
$handler: {▼
typeHint: "callable"
}
}
file: "/home/user/mysite/vendor/guzzlehttp/guzzle/src/Middleware.php"
line: "27 to 43"
}
1 => "cookies"
]
3 => array:2 [▼
0 => Closure {#286 ▼
class: "GuzzleHttp\Middleware"
parameters: {▼
$handler: {▼
typeHint: "callable"
}
}
file: "/home/user/mysite/vendor/guzzlehttp/guzzle/src/Middleware.php"
line: "216 to 218"
}
1 => "prepare_body"
]
]
-cached: null
}
"allow_redirects" => array:5 [▼
"max" => 5
"protocols" => array:2 [▼
0 => "http"
1 => "https"
]
"strict" => false
"referer" => false
"track_redirects" => false
]
"http_errors" => true
"decode_content" => true
"verify" => true
"cookies" => false
"headers" => array:1 [▼
"User-Agent" => "GuzzleHttp/6.2.1 PHP/7.0.8-0ubuntu0.16.04.3"
]
]
}
测试您的令牌和参数的一个好方法是导航到 RapidAPI here. 我已将您直接链接到 EventbriteAPI updateEvent 端点。您应该继续将参数和您的令牌填写到表格中,然后单击测试。您应该会看到详细的 JSON 回复。这是一个简单的例子:
如果一切正常,请单击仪表板中响应正上方的代码,注册,从下拉列表中选择 PHP,RapidAPI 将提供 php 代码片段,您只需将其复制并直接粘贴到您的代码中即可进行 API 调用。下面是 RapidAPI 将提供的 PHP 代码片段示例:
require __DIR__ . '/vendor/autoload.php';
use RapidApi\rapidApiConnect;
$rapid = new RapidApiConnect("WhosebugTest", "##########################");
$result = $rapid->call('EventbriteAPI', 'updateEvent', [
'token' => '####################',
'eventId' => '31082267900',
'eventName' => 'Ghost Tour Test',
'eventStartUtc' => '2017-02-20T03:00:00Z',
'eventStartTimezone' => 'America/Los_Angeles',
'eventEndUtc' => '2017-02-20T06:00:00Z',
'eventEndTimezone' => 'America/Los_Angeles',
'eventCurrency' => 'USD',
'eventDescription' => ''
]);
希望对您有所帮助!
您很可能应该为 Guzzle 使用 body
请求选项而不是 form_params
。对请求使用 form_params
将发送 application/x-www-form-urlencoded
数据,这通常是 而不是 现代 API 所期望的。对于 Eventbrite,我相信它需要一个常规的 form-data
编码请求。
将您的 Guzzle 请求更改为:
$res = $client->request('POST', "events/$this->EventBriteID", [
'query' => ['token' => env('EVENTBRITE_TOKEN', '')],
'body' => $params
]);
希望这有效!
所以经过几个小时的绝望,我终于修好了。问题是端点 url:
上缺少尾部斜线$res = $client->request('POST', "events/$this->EventBriteID", []);
改为
$res = $client->request('POST', "events/$this->EventBriteID/", []);
现在可以正常工作了。