1

I have code which can POST a JSON to an API using authentication. I need to GET using authentication, yet most examples do not provide this information.

POST

public function putTest() { $username = "user"; $password = "pass"; $json_url = "http://api.of.site.com"; $json_string = "[]"; $ch = curl_init( $json_url ); $options = array( CURLOPT_RETURNTRANSFER => true, CURLOPT_USERPWD => "{$username}:{$password}", CURLOPT_HTTPHEADER => array( "Content-type: application/json" ), CURLOPT_POSTFIELDS => $json_string ); curl_setopt_array( $ch, $options ); $result = curl_exec( $ch ); } 

GET (not working)

public function getTest() { $username = "user"; $password = "pass"; $json_url = "http://api.of.site.com"; $ch = curl_init( $json_url ); $options = array( CURLOPT_RETURNTRANSFER => true, CURLOPT_USERPWD => "{$username}:{$password}", CURLOPT_HTTPHEADER => array( "Content-type: application/json" ), ); curl_setopt_array( $ch, $options ); $result = curl_exec( $ch ); } 

What am I missing in my GET? Also I would like to keep it in this style rather than using curl_setopt

EDIT: As reference, here is the command line version which works

curl -X GET -H "Content-Type: application/json" -H "Accept: application/json" -u "username:pass" "http://api.site.name.com/thingtoget" 

3 Answers 3

1

You need to use CURLOPT_HTTPAUTH

Use CURLOPT_HTTPAUTH to specify the authentication method for HTTP based connections

http://curl.haxx.se/libcurl/c/CURLOPT_USERPWD.html

Sign up to request clarification or add additional context in comments.

1 Comment

Tried this, I think I'm missing something else
1

You need to add:

curl_setopt($curl_handle, CURLOPT_USERPWD, $username . ':' . $password); 

An example:

public function call(array $data) { $payload = json_encode($data['payload']); // Initialise the session $curl = curl_init(); // Set API URL curl_setopt($curl, CURLOPT_URL, $data['url']); // Return the transfer as a string of the return value of curl_exec() instead of outputting it out directly curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // Include the header in the output curl_setopt($curl, CURLOPT_HEADER, true); // Authentication curl_setopt($curl_handle, CURLOPT_USERPWD, 'your_username:and_password'); // Set request method curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $data['method']); if ($data['method'] == 'POST') { // Do a regular HTTP POST curl_setopt($curl, CURLOPT_POST, true); } if ($data['method'] == 'POST' || $data['method'] == 'PUT') { // The full data to post in a HTTP POST operation curl_setopt($curl, CURLOPT_POSTFIELDS, $payload); curl_setopt($curl, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($payload) )); } // Execute the request $output = curl_exec($curl); // Close curl resource to free up system resources curl_close($curl); // If connection failed if (! $output) { return 'Curl connection failure.'; } // Output the profile information, includes the header return $output; } 

Comments

1

So the POST needs CURLOPT_HTTPHEADER => array( "Content-type: application/json" ), while the GET needs CURLOPT_HTTPHEADER => array( "Accept: application/json" ), Makes sense reading it now.

public function getTest() { $username = "user"; $password = "pass"; $json_url = "http://api.of.site.com"; $ch = curl_init( $json_url ); $options = array( CURLOPT_SSL_VERIFYPEER => false, CURLOPT_RETURNTRANSFER => true, CURLOPT_USERPWD => "{$username}:{$password}", CURLOPT_HTTPHEADER => array( "Accept: application/json" ), ); curl_setopt_array( $ch, $options ); $result = curl_exec( $ch ); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.