带有授权的PHP Rest API调用

时间:2014-06-20 21:24:19

标签: php api rest authorization

简单的问题

我会在哪里放置"授权:持票人cajwune0fnr78ynj2kz6p8bad"

$service_url = 'https://example.com/something/something.json';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "username:password"); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
$curl_response = curl_exec($curl);
$response = json_decode($curl_response);
curl_close($curl);

var_dump($response);

谢谢!

1 个答案:

答案 0 :(得分:2)

你会把它放进去:

***老

curl_setopt($curl, CURLOPT_HTTPHEADER, 'Authorization: Bearer cajwune0fnr78ynj2kz6p8bad');

***新

curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Bearer cajwune0fnr78ynj2kz6p8bad'));