将 JSON 转换为 guzzle php 图书馆请求
convert JSON to guzzle php librairy request
我正在尝试将 curl 隐藏到 guzzle 请求,这是 curl 请求。
curl https://{subdomain}.zendesk.com/api/v2/tickets.json \
-d '{"ticket": {"subject": "My printer is on fire!", "comment": { "body": "The smoke is very colorful." }}}' \
-H "Content-Type: application/json" -v -u {email_address}:{password} -X POST
这里是 JSON 部分:
{
"ticket": {
"requester": {
"name": "The Customer",
"email": "thecustomer@domain.com"
},
"subject": "My printer is on fire!",
"comment": {
"body": "The smoke is very colorful."
}
}
}
这是我损坏的 PHP 代码。
$client = new GuzzleHttp\Client();
$res = $client->post('https://midnetworkshelp.zendesk.com/api/v2/tickets/tickets.json', [
'query' => [
'ticket' => ['subject' => 'My print is on Fire'], [
'comment' => [
'body' => 'The smoke is very colorful'] ], 'auth' => ['email', 'Password']]);
echo $res->getBody();
我一直在未经授权访问用户,但是当我启动 curl 命令时,它工作正常。
知道我在这里可能遗漏了什么吗?
谢谢
你不应该使用查询参数,因为你需要发送原始 json 作为请求的主体(不像你正在做的那样在参数中。)检查 here for information on how to accomplish this. Also, be sure to try to enable debugging 到弄清楚为什么请求没有按您想要的方式发布。 (您可以比较 curl 和 guzzles 的调试输出以验证它们匹配)。
参考:
- http://curl.haxx.se/docs/manpage.html
- http://guzzle.readthedocs.org/en/latest/clients.html
- https://github.com/guzzle/log-subscriber
- http://guzzle.readthedocs.org/en/latest/clients.html#json
您最大的问题是您没有正确转换您的 curl 请求。
- -d = 正在发布的数据。换句话说,这是您请求的body。
- -u = 用于验证您的请求的 username:pw。
- -H = 您希望在请求中使用的额外 header。
- -v = 详细输出。
- -X = 指定请求方式。
我建议按如下方式实例化您的客户端:
$client = new GuzzleHttp\Client([
'base_url' => ['https://{subdomain}.zendesk.com/api/{version}/', [
'subdomain' => '<some subdomain name>',
'version' => 'v2',
],
'defaults' => [
'auth' => [ $username, $password],
'headers' => ['Content-Type' => 'application/json'], //only if all requests will be with json
],
'debug' => true, // only for debugging purposes
]);
这将:
- 确保向 api 发出的多个后续请求将具有身份验证信息。使您不必将其添加到每个请求中。
- 确保使用此客户端发出的多个后续(实际上是所有)请求将包含指定的 header。使您不必将其添加到每个请求中。
- 提供一定程度的未来证明(将子域和 api 版本移动到可编辑字段中)。
如果您选择记录您的请求和响应 objects,您还可以:
// You can use any PSR3 compliant logger in space of "null".
// Log the full request and response messages using echo() calls.
$client->getEmitter()->attach(new GuzzleHttp\Subscriber\Log\LogSubscriber(null, GuzzleHttp\Subscriber\Log\Formatter::DEBUG);
您的请求将简单地变为:
$json = '{"ticket": {"subject": "My printer is on fire!", "comment": { "body": "The smoke is very colorful." }}}';
$url = 'tickets/tickets.json';
$request = $client->createRequest('POST', $url, [
'body' => $json,
]);
$response = $client->send($request);
或
$json = '{"ticket": {"subject": "My printer is on fire!", "comment": { "body": "The smoke is very colorful." }}}';
$url = 'tickets/tickets.json';
$result = $client->post(, [
'body' => $json,
]);
编辑:
在更深入地阅读参考文献 4 之后,应该可以执行以下操作:
$url = 'tickets/tickets.json';
$client = new GuzzleHttp\Client([
'base_url' => ['https://{subdomain}.zendesk.com/api/{version}/', [
'subdomain' => '<some subdomain name>',
'version' => 'v2',
],
'defaults' => [
'auth' => [ $username, $password],
],
'debug' => true, // only for debugging purposes
]);
$result = $client->post($url, [
'json' => $json, // Any PHP type that can be operated on by PHP’s json_encode() function.
]);
我正在尝试将 curl 隐藏到 guzzle 请求,这是 curl 请求。
curl https://{subdomain}.zendesk.com/api/v2/tickets.json \
-d '{"ticket": {"subject": "My printer is on fire!", "comment": { "body": "The smoke is very colorful." }}}' \
-H "Content-Type: application/json" -v -u {email_address}:{password} -X POST
这里是 JSON 部分:
{
"ticket": {
"requester": {
"name": "The Customer",
"email": "thecustomer@domain.com"
},
"subject": "My printer is on fire!",
"comment": {
"body": "The smoke is very colorful."
}
}
}
这是我损坏的 PHP 代码。
$client = new GuzzleHttp\Client();
$res = $client->post('https://midnetworkshelp.zendesk.com/api/v2/tickets/tickets.json', [
'query' => [
'ticket' => ['subject' => 'My print is on Fire'], [
'comment' => [
'body' => 'The smoke is very colorful'] ], 'auth' => ['email', 'Password']]);
echo $res->getBody();
我一直在未经授权访问用户,但是当我启动 curl 命令时,它工作正常。
知道我在这里可能遗漏了什么吗?
谢谢
你不应该使用查询参数,因为你需要发送原始 json 作为请求的主体(不像你正在做的那样在参数中。)检查 here for information on how to accomplish this. Also, be sure to try to enable debugging 到弄清楚为什么请求没有按您想要的方式发布。 (您可以比较 curl 和 guzzles 的调试输出以验证它们匹配)。
参考:
- http://curl.haxx.se/docs/manpage.html
- http://guzzle.readthedocs.org/en/latest/clients.html
- https://github.com/guzzle/log-subscriber
- http://guzzle.readthedocs.org/en/latest/clients.html#json
您最大的问题是您没有正确转换您的 curl 请求。
- -d = 正在发布的数据。换句话说,这是您请求的body。
- -u = 用于验证您的请求的 username:pw。
- -H = 您希望在请求中使用的额外 header。
- -v = 详细输出。
- -X = 指定请求方式。
我建议按如下方式实例化您的客户端:
$client = new GuzzleHttp\Client([
'base_url' => ['https://{subdomain}.zendesk.com/api/{version}/', [
'subdomain' => '<some subdomain name>',
'version' => 'v2',
],
'defaults' => [
'auth' => [ $username, $password],
'headers' => ['Content-Type' => 'application/json'], //only if all requests will be with json
],
'debug' => true, // only for debugging purposes
]);
这将:
- 确保向 api 发出的多个后续请求将具有身份验证信息。使您不必将其添加到每个请求中。
- 确保使用此客户端发出的多个后续(实际上是所有)请求将包含指定的 header。使您不必将其添加到每个请求中。
- 提供一定程度的未来证明(将子域和 api 版本移动到可编辑字段中)。
如果您选择记录您的请求和响应 objects,您还可以:
// You can use any PSR3 compliant logger in space of "null".
// Log the full request and response messages using echo() calls.
$client->getEmitter()->attach(new GuzzleHttp\Subscriber\Log\LogSubscriber(null, GuzzleHttp\Subscriber\Log\Formatter::DEBUG);
您的请求将简单地变为:
$json = '{"ticket": {"subject": "My printer is on fire!", "comment": { "body": "The smoke is very colorful." }}}';
$url = 'tickets/tickets.json';
$request = $client->createRequest('POST', $url, [
'body' => $json,
]);
$response = $client->send($request);
或
$json = '{"ticket": {"subject": "My printer is on fire!", "comment": { "body": "The smoke is very colorful." }}}';
$url = 'tickets/tickets.json';
$result = $client->post(, [
'body' => $json,
]);
编辑: 在更深入地阅读参考文献 4 之后,应该可以执行以下操作:
$url = 'tickets/tickets.json';
$client = new GuzzleHttp\Client([
'base_url' => ['https://{subdomain}.zendesk.com/api/{version}/', [
'subdomain' => '<some subdomain name>',
'version' => 'v2',
],
'defaults' => [
'auth' => [ $username, $password],
],
'debug' => true, // only for debugging purposes
]);
$result = $client->post($url, [
'json' => $json, // Any PHP type that can be operated on by PHP’s json_encode() function.
]);