1

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,
                    ],
                ],
            ],
        ],
    ]);
  • Can you show the documentation of this specific endpoint of the Vertifire API? – piscator Jul 20 '17 at 09:42
  • the headers should be array of key and value[['X-Vertifire-Token' => '****']] – Ameer Salah Aldeen Jul 20 '17 at 09:42
  • @piscator Unfortunately this is a premium account and there is no public link available but what I see is: _The request accepts up to 10 entries. Each entry is a combination of the following required fields: term (string), url (string) and sep (set - search_engine)_ – James Blackmore Jul 20 '17 at 10:03
  • @AmeerSalahAldeen The headers seem ok since the settings are stored at Laravel's config file and I can make a GET requests – James Blackmore Jul 20 '17 at 10:06

1 Answers1

0

Try to change your request to this

$request = $client->post('v2/serp/url', [
    'headers'   => [
        'X-Vertifire-Token' => config('guzzleapi.vertifire_auth'),
    ] 
    'form_params' => [
        'term' => 'videos',
        'url' => 'wikipedia.org',
        'sep' => 1,
    ]
]);

Let me know if it works, or what response you get.

piscator
  • 8,028
  • 5
  • 23
  • 32
  • Thanks! Same error as before: _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"}}_ – James Blackmore Jul 20 '17 at 10:14
  • @JamesBlackmore Try to change 'data' to 'form_params' (see updated answer). I also changed the header, although that probably isn't the cause of the issue. It's hard without documentation, but I hope it helps you. – piscator Jul 20 '17 at 11:09
  • Thanks for your effort, I figured it out! I edited my answer appending the solution. – James Blackmore Jul 20 '17 at 13:32