-1

I am trying to set 2 variables to the curl headers and can't seem to see what's wrong. I am not receiving any errors in the php logs, but when I print out the curl info I can see that the headers are not being set. Any point in the right direction would be helpful. Thanks

The Example I'm using PHP cURL custom headers

class GetAuctions
{
private $APIKeyID = "theIDhere";
private $APIKeyPass = "thePasswordHere";
private $BaseURL = "https://someurlHere";

public function __construct()
{
    //get list of upcoming auctions 
    $get_data = $this->callAPI('GET', $this->BaseURL, false);
    //turn the response into a json
    $response = json_decode($get_data, true);
    //display the response for testing
    echo print_r($response);
    $errors = $response['response']['errors'];
    $data = $response['response']['data'][0];
    echo print_r($data);
}

function callAPI($method, $url, $data){
    $curl = curl_init();

    switch ($method){
        case "POST":
            curl_setopt($curl, CURLOPT_POST, 1);
            if ($data)
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            break;
        case "PUT":
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
            if ($data)
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            break;
        default:
            if ($data)
                $url = sprintf("%s?%s", $url, http_build_query($data));
    }

    // OPTIONS:
    curl_setopt($curl, CURLOPT_URL, $url);
    $headers =array();
    $headers['apiKeyID'] = $this->APIKeyID;
    $headers['apiKeyPass'] = $this->APIKeyPass;
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    echo "<br/>";
    echo print_r(curl_getinfo($curl));
    echo "<br/>";
    // EXECUTE:
    $result = curl_exec($curl);
    if(!$result){die("Connection Failure");}
    curl_close($curl);
    return $result;
}
}

I've also tried this:

curl_setopt($curl, CURLOPT_HTTPHEADER, array(
        "apiKeyID: $this->APIKeyID",
        "apiKeyPass: $this->APIKeyPass"
    ));

My response looks like this:

Array ( [url] => https://MyURLHere [content_type] => [http_code] => 0 [header_size] => 0 [request_size] => 0 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0 [namelookup_time] => 0 [connect_time] => 0 [pretransfer_time] => 0 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => -1 [upload_content_length] => -1 [starttransfer_time] => 0 [redirect_time] => 0 [redirect_url] => [primary_ip] => [certinfo] => Array ( ) [primary_port] => 0 [local_ip] => [local_port] => 0 ) 1

Connection Failure

Sari Rahal
  • 1,897
  • 2
  • 32
  • 53

1 Answers1

1

It looks like your headers are incorrectly formatted:

"apiKeyID : $this->APIKeyID",

You should remove space before your colon:

"apiKeyID: ${this->APIKeyID}",

BTW: for debugging purpose you may also use CURLOPT_VERBOSE to see what's is being send by cURL. If you cannot peek stderr at runtime, redirect it to the file:

curl_setopt($c, CURLOPT_STDERR, fopen('curl-log.txt', 'w+'));
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141