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"