Skip to content

Instantly share code, notes, and snippets.

@4cm
Created July 6, 2019 10:01
Show Gist options
  • Save 4cm/b5fae5bd10430c5603bc0811efd047df to your computer and use it in GitHub Desktop.
Save 4cm/b5fae5bd10430c5603bc0811efd047df to your computer and use it in GitHub Desktop.
php curl method to get content of a json request, while using an Authorization Bearer httpheader option.
/**
* @param $url
* @param $token
* @return bool|string
*/
function curl_GetDataWithAuthorizationBearer($url, $token) {
//
$cookie = tmpfile();
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer ' . $token));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
return curl_exec($ch);
//
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment