将JSON转换为guzzle php librairy请求

时间:2015-03-27 19:19:40

标签: php json curl guzzle

我正试图将一个卷曲转换为guzzle请求,这是卷曲请求。

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命令时,它工作正常。

对于我在这里可能缺少的东西有任何想法吗?

谢谢

2 个答案:

答案 0 :(得分:2)

参考:

  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
  5. 您最大的问题是您没有正确转换卷曲请求。

    • -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. 确保使用此客户端进行的multipl后续(实际上所有)请求将包含指定的标头。使您不必将其添加到每个请求中。
    3. 提供一定程度的未来校对(将子域和api版本移动到可编辑字段中)。
    4. 如果您选择记录您的请求和响应对象,您也可以这样做:

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

答案 1 :(得分:1)

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