1

I'm trying to covert a curl to guzzle request, here is the curl request.

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

here is the JSON portion:

{
    "ticket": {
        "requester": {
            "name": "The Customer",
            "email": "thecustomer@domain.com"
        },
        "subject": "My printer is on fire!",
        "comment": {
            "body": "The smoke is very colorful."
        }
    }
}

Here is my broken PHP code.

$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();

I keep getting access unauthorized for the user, however when I fire the curl command it works fine.

Any idea on what I'm possibly missing here?

Thank you

Machavity
  • 30,841
  • 27
  • 92
  • 100
Deano
  • 11,582
  • 18
  • 69
  • 119

2 Answers2

2

Ref:

  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

Your biggest issue is that you are not converting your curl request properly.

  • -d = data that is being posted. In other words this is the body of your request.
  • -u = the username:pw that is being used to authenticate your request.
  • -H = extra headers that you want to use within your request.
  • -v = verbose output.
  • -X = specifies the request method.

I would recommend instanciating your client as follows:

$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
]);

This will:

  1. Ensure that multiple subsequent requests made to the api will have the authentication information. Saving you from having to add it to each and every request.
  2. Ensure that multipl subsequent (actually all) requests made with this client will contain the specified header. Saving you from having to add it to each and every request.
  3. Provides some degree of future proofing (moving subdomain and api version into editable fields).

If you choose to log your request and response objects you can also do:

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

Your request will then simply become:

$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);

or

$json = '{"ticket": {"subject": "My printer is on fire!", "comment": { "body": "The smoke is very colorful." }}}';
$url = 'tickets/tickets.json';

$result = $client->post(, [
        'body'  => $json,
]);

Edit: After futher reading Ref 4 more thouroughly it should be possible to do the following:

$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.
]);
Shaun Bramley
  • 1,989
  • 11
  • 16
1

You shouldn't be using the query parameter, as you need to send the raw json as the body of the request (Not in parameters like you are doing.) Check here for information on how to accomplish this. Also, be sure to try to enable debugging to figure out why a request isn't posting like you want. (You can compare both curl and guzzles debug output to verify they match).

Community
  • 1
  • 1