1

I'm having trouble trying to get my code running. I'm trying to use cUrl with a post and http authentication but I'm unable to get it to work.

I'm using the following code:

public function index(){
    $call = $this->call("URL_TO_CALL", $this->getCredentials(), $this->getJson());      
}

private function getCredentials(){
    return "API_KEY_HERE";
}

private function getJson(){
    return $json;
}

private function call($page, $credentials, $post){

    $url = "URL_TO_CALL"; 
    $page = "/shipments"; 
    $headers = array(  
        "Content-type: application/vnd.shipment+json;charset=utf-8", 
        "Authorization: Basic :" . base64_encode($credentials) 
    ); 

    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL,$url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 60); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 

    // Apply the POST to our curl call 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 

    $data = curl_exec($ch); 

    if (curl_errno($ch)) { 
        print "Error: " . curl_error($ch); 
    } else { 
        // Show me the result 
        var_dump($data); 
        curl_close($ch); 
    }
}

The response I'm getting is:

{"errors":[{"code":3001}],"message":"Permission Denied. (insertShipment)"}

I was expecting a list of shipments. Am I doing something obviously wrong here?

killstreet
  • 1,251
  • 2
  • 15
  • 37
  • should be "Authorization: Basic " . base64_encode($credentials) (without the `:` after the Basic) – Yair.R Mar 17 '16 at 09:52
  • @Yair.R If I do it without the double dots I get the following response: `{"errors":[{"code":3001}],"message":"Permission denied"}` – killstreet Mar 17 '16 at 09:53
  • So it should be without the double dots, and if it still doesn't work - your credentials are wrong – Yair.R Mar 17 '16 at 09:55
  • Having no double dots seem to return a general error message, having the doule dots seem to include the function name that's being called, shouldnt that just mean I need to include the dots? – killstreet Mar 17 '16 at 09:56
  • You can use the following link for reference: http://stackoverflow.com/questions/2140419/how-do-i-make-a-request-using-http-basic-authentication-with-php-curl – Indrajit Mar 17 '16 at 09:57

1 Answers1

0

Looking at your code, you stipulate the URL in a variable then stipulate the exact page, however when you set the URL for the curl, you are merely using the URL variable as opposed to URL + PAGE variables.

Jim Grant
  • 1,128
  • 2
  • 13
  • 31
  • It's because it's just testing code, the url that's being used still looks like: `https://api.website.com/shipments` – killstreet Mar 17 '16 at 09:49