【问题标题】:convert JSON to guzzle php librairy request将 JSON 转换为 guzzle php 库请求
【发布时间】:2015-06-01 05:57:33
【问题描述】:

我正在尝试将 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 命令时,它可以正常工作。

知道我在这里可能缺少什么吗?

谢谢

【问题讨论】:

    标签: php json curl guzzle


    【解决方案1】:

    参考:

    1. http://curl.haxx.se/docs/manpage.html
    2. http://guzzle.readthedocs.org/en/latest/clients.html
    3. https://github.com/guzzle/log-subscriber
    4. http://guzzle.readthedocs.org/en/latest/clients.html#json

    您最大的问题是您没有正确转换 curl 请求。

    • -d = 正在发布的数据。换句话说,这是您的请求正文。
    • -u = 用于验证您的请求的用户名:pw。
    • -H = 要在请求中使用的额外标头。
    • -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
    ]);
    

    这将:

    1. 确保对 api 发出的多个后续请求将具有身份验证信息。让您不必将其添加到每个请求中。
    2. 确保使用此客户端发出的多个后续(实际上是所有)请求将包含指定的标头。让您不必将其添加到每个请求中。
    3. 提供一定程度的未来验证(将子域和 api 版本移动到可编辑字段中)。

    如果您选择记录您的请求和响应对象,您也可以这样做:

    // 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,
    ]);
    

    编辑: 在进一步阅读 Ref 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.
    ]);
    

    【讨论】:

      【解决方案2】:

      您不应该使用查询参数,因为您需要将原始 json 作为请求的主体发送(不是像您正在做的那样在参数中。)查看here 以获取有关如何完成此操作的信息。此外,请务必尝试enable debugging 以找出请求未按您想要的方式发布的原因。 (您可以比较 curl 和 guzzles 调试输出以验证它们是否匹配)。

      【讨论】:

        猜你喜欢
        • 2021-11-30
        • 2015-11-02
        • 1970-01-01
        • 2016-12-04
        • 2017-01-13
        • 2019-12-17
        • 2018-11-04
        • 1970-01-01
        • 2017-04-04
        相关资源
        最近更新 更多