I'm trying to use Guzzle with Laravel to make a post request using Vertifire API. According to Vertifire's documentation, a minimal post request should be:
curl https://api50.vertifire.com/v2/serp/url \
--header "X-Vertifire-Token: MY TOKEN HERE" \
--data terms[0][term]=videos \
--data terms[0][url]=wikipedia.org \
--data terms[0][sep][search_engine]=1
Following Guzzle's documentation, firstly I create a client:
$client = new Client(['base_uri' => 'https://api50.vertifire.com/']);
And I'm stuck in making the actual request:
$request = $client->post('v2/serp/url', [
'headers' => config('guzzleapi.vertifire_auth'), // Laravel's config file with token
'data' => [
$terms[0]['term'] = 'videos',
$terms[0]['url'] = 'wikipedia.org',
$terms[0]['sep']['search_engine'] = 1,
]
]);
Which results with the error:
Client error: `POST https://api50.vertifire.com/v2/serp/url` resulted in a `400 Bad Request`
response: {"status":0,"error":{"code":3001,"message":"Missing term"}}
Obviously I'm messing up with the way I pass data to the request... How can I pass specific data or array of data to a post request using Guzzle? Any help will be really appreciated since I'm a rookie in this field.
Thanks!
SOLVED / SOLUTION
It seems the proper way to pass data to the request is with data of the array expanded like this:
$ranks_request = $client->post('v2/serp/url', [
'headers' => config('guzzleapi.vertifire_auth'),
'form_params' => [
'terms' => [
[
'term' => 'videos',
'url' => 'wikipedia.org',
'sep' => [
'search_engine' => 1,
],
],
],
],
]);