0

I am trying to get a response from the Metals API but keep getting 404 errors even though I can get the API using the URL.

    public function valueFromApi(){

    $accesskey = "123456";
    $client = new \GuzzleHttp\Client();
    $response = $client->request('POST', 'https://metals-api.com/api/latest', [
        'form_params' => [
            'access_key' => $accesskey,
            'base' => 'GBP',
            'symbols' => 'XAU',]
]);

dd($response);

}

If I try and access the URL directly through a browser this works:

https://metals-api.com/api/latest?access_key=123456&base=GBP&symbols=XAU

I must have misunderstood the way the parameters are working. Any advice is appreciated.

2 Answers 2

1

Form params is not the same as query parameters. Therefor you need to set the parameters as query. If you are accessing this in the browser, i would not expect it to be a POST but a GET.

$response = $client->request('GET', 'https://metals-api.com/api/latest', [
    RequestOptions::QUERY => [
        'access_key' => $accesskey,
        'base' => 'GBP',
        'symbols' => 'XAU',
    ]
]);

I am using the RequestOptions, this is syntaxic sugar for the hardcoded string options, the same as 'query'.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks that worked but I also added "use GuzzleHttp\RequestOptions"
Ofc that is implicit in my answer ;)
0

As specified in their docs, you need to define the constant

    define("form_params", GuzzleHttp\RequestOptions::FORM_PARAMS );

Then you can use your code

$response = $client->request('POST', 'https://metals-api.com/api/latest', [
        'form_params' => [
            'access_key' => $accesskey,
            'base' => 'GBP',
            'symbols' => 'XAU',]
]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.