POST update data to EventBrite API using Guzzle

942 views Asked by At

I'm able to add Events using post with the following code:

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

However when I try to update the event using the ID created in step one and the following code:

//Send update event request
$res = $client->request('POST', "events/$this->EventBriteID", [
  'query' => ['token' => env('EVENTBRITE_TOKEN', '')],
  'form_params' => $params
]);

It doesn't update the event. The request looks successful. Status 200 is returned a long with the event object, however none of the data is updated. The information stays the same as was submitted when the event was created. What am I doing wrong, I'm finding it hard to debug because the endpoint is returning a 200 status.

The request works fine in Postman, so i'm obviously doing something wrong in Guzzle.

EDIT

As requested output of $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"
    ]
  ]
}
3

There are 3 answers

2
Alex On BEST ANSWER

So after many hours of despair, I finally fixed it. The issue was a missing trailing slash on the endpoint url:

$res = $client->request('POST', "events/$this->EventBriteID", []);

changed to

$res = $client->request('POST', "events/$this->EventBriteID/", []);

Now works as expected.

3
samrap On

It most likely that you should be using the body request option for Guzzle rather than form_params. Using form_params for a request will send application/x-www-form-urlencoded data which is typically not what modern APIs expect. In the case of Eventbrite I believe it expects a regular form-data encoded request.

Change your Guzzle request to:

$res = $client->request('POST', "events/$this->EventBriteID", [
  'query' => ['token' => env('EVENTBRITE_TOKEN', '')],
  'body' => $params
]);

Hope this works!

0
David Noah On

A good way to test your token and parameters would be to navigate to RapidAPI here. I've linked you directly to the EventbriteAPI updateEvent endpoint. You should go ahead and fill in the parameters and your token into the form and click test. You should see a detailed JSON response. Heres a quick example:

enter image description here

If everything looks like it has worked properly, click CODE right above the response in the dashboard, sign up, choose PHP from the dropdown, and RapidAPI will provide a php code snippet that you can just copy and paste directly into your code to make the API call. Heres an example of the PHP code snippet that RapidAPI will provide:

require __DIR__ . '/vendor/autoload.php';
use RapidApi\rapidApiConnect;

$rapid = new RapidApiConnect("StackOverflowTest",       "##########################");

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

Hope this helps!