您可以使用 curl 发送标头,如下所示:
$service_url = 'https://api.familysearch.org/reservation/v1/person/L81G-M81';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "username:password"); //Your credentials here
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, true);
$curl_res = curl_exec($curl);
$response = json_decode($curl_res);
curl_close($curl);
var_dump($response);
确认您已为 ssl 连接启用 openssl 模块。
编辑:
这里发送带有令牌的标头是一个 OAuth 示例。
$headr = array();
$headr[] = 'Content-length: 0';
$headr[] = 'Content-type: application/json';
$headr[] = 'Authorization: OAuth '.$accesstoken;
curl_setopt($crl, CURLOPT_HTTPHEADER,$headr);
要测试命令,您可以使用命令行:
curl -H "Authorization: Bearer <ACCESS_TOKEN>" http://www.example.com