'Basic Authentication with CURL/PHP

I'm trying to do a basic API Authentication using PHP and CURL and I keep getting the error "no Route matched with those values".

Here is what the API docs are telling me to do:

curl 'https://api.impact.com/Advertiser/<AccountSID>/Actions' \
-u '<AccountSID>:<AuthToken>'

And here is the PHP i'm using to try and do this:

$accountSID = 'XXXXX';
$authToken = 'XXXXX';
$url = 'https://api.impact.com/Advertiser/'.$accountSID.'/Actions';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $accountSID.":".$authToken);
$result = curl_exec($ch);

curl_close($ch);  
echo($result);

What am I missing?



Solution 1:[1]

Don't know if it's gonna solve your issue but you can try:

$username = 'name';
$password = 'pass';
$headers = array(
    'Method: GET',
    'Authorization: Basic '. base64_encode($username.':'.$password)
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

Solution 2:[2]

"no Route matched with those values" means your URL and/or method is off. It's not the auth that's faulty, it's the /Advertiser/<AccountSID>/Actions part that you should check.

In general, in Web systems where the URL path tree doesn't correspond to the underlying filesystem (like most APIs), routing refers to the process of finding the right handler (e. g. a class or a function) for the provided method and URL path, which in your case is GET to /Advertiser/<AccountSID>/Actions. I'd rather expect the URL that ends with Actions to accept a POST, not a GET, but you should check with the API docs and/or support.

Depending on how the server is set up, this error may or may not mean that the authentication went fine. A server may run authentication before routing, or after.

Solution 3:[3]

Turns out I had the endpoint URL wrong. it should have been

$url = 'https://api.impact.com/Advertisers/'.$accountSID.'/Actions';

instead of

$url = 'https://api.impact.com/Advertiser/'.$accountSID.'/Actions';

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kip
Solution 2 Seva Alekseyev
Solution 3 Daelan