1

I am trying to pull in data from an API using HTTP Basic authentication. The HTTP requests to the API are protected with HTTP Basic authentication. HTTP Basic authentication consists of a token and secret.

I have tried many different techniques, but keep getting the response that authentication was not provided. I am not sure if the token:secret method is different from username:password but I cannot get this to authenticate.

stdClass Object ( [error_message] => Authentication not provided. )

Here is the API documentation - https://www.whatconverts.com/api/

<?php


$token = "xxx";
$secret = "yyy";
$response = get_web_page("https://leads.seekmomentum.com/api/v1/leads");
$resArr = array();
$resArr = json_decode($response);
echo "<pre>"; print_r($resArr); echo "</pre>";

function get_web_page($url) {
    $options = array(
        CURLOPT_RETURNTRANSFER => true,   // return web page
        CURLOPT_HEADER         => false,  // don't return headers
        CURLOPT_FOLLOWLOCATION => true,   // follow redirects
        CURLOPT_MAXREDIRS      => 10,     // stop after 10 redirects
        CURLOPT_ENCODING       => "",     // handle compressed
        CURLOPT_USERAGENT      => "test", // name of client
        CURLOPT_AUTOREFERER    => true,   // set referrer on redirect
        CURLOPT_CONNECTTIMEOUT => 120,    // time-out on connect
        CURLOPT_TIMEOUT        => 120,    // time-out on response
        CURLOPT_HTTPAUTH       => "CURLAUTH_BASIC",  // authentication method
        CURLOPT_USERPWD        => "$token:$secret",  // authentication

    ); 


    $ch = curl_init($url);
    curl_setopt_array($ch, $options);

    $content  = curl_exec($ch);

    curl_close($ch);

    return $content;
}

?>
3
  • Have you tried separate CURLOPT_USERNAME and CURLOPT_PASSWORD? Commented Jul 13, 2016 at 15:36
  • 1
    Thanks for your credentials... might want to go get those changed/invalidated immediately. Commented Jul 13, 2016 at 15:37
  • 1
    Remove the quotes around CURLAUTH_BASIC - it's a constant, not a value. Commented Jul 13, 2016 at 15:39

2 Answers 2

3

This is wrong:

    CURLOPT_HTTPAUTH       => "CURLAUTH_BASIC",  // authentication method
                              ^^^^^^^^^^^^^^^^

That's a string, not a curl constant. Try

    CURLOPT_HTTPAUTH       => CURLAUTH_BASIC,  // authentication method

instead.

it's the difference between:

define('FOO', 'bar');

echo FOO // outputs bar
echo "FOO" // outputs FOO
Sign up to request clarification or add additional context in comments.

Comments

0

You need to pass your global variables into the local scope. To do this...

Change:

function get_web_page($url) {

To:

function get_web_page( $url, $token, $secret ) {

and change:

$response = get_web_page("https://leads.seekmomentum.com/api/v1/leads");

To:

$response = get_web_page( "https://leads.seekmomentum.com/api/v1/leads", $token, $secret );

and:

Remove the quotes around CURLAUTH_BASIC - it's a constant, not a value. (hat tip to @iainn)

2 Comments

Thanks Ben! That did it.
@user2748363 Glad I could help. Please select my answer as the answer to the question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.