Home > Enterprise >  Basic Authentication with CURL/PHP
Basic Authentication with CURL/PHP

Time:05-06

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?

CodePudding user response:

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);

CodePudding user response:

"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.

  • Related