[英]Sending custom HTTP request with PHP
我正在嘗試使用新的Office 365 API進行身份驗證。 我已經成功通過身份驗證,並且正在玫瑰花園里游泳。
但是,最后一部分是發送帶有我閃亮的新訪問令牌的自定義HTTP請求。 它似乎沒有用。
有人可以概述一下如何使用PHP發送下面詳細介紹的http請求嗎?
GET https://outlook.office365.com/EWS/OData/Me/Inbox/Messages?$top=5
User-Agent: My-Cool-WebApp/1.0
client-request-id: 9aa1c740-25e2-4841-9146-ef7018f7bf37
return-client-request-id: true
authorization: Bearer {access token your app received in Step Three}
謝謝
下面的代碼:
$code = $_GET['code'];
$accessToken = AccessCode::getCode($code);
$EmailData = EmailSearch::getEmail($accessToken);
class AccessCode
{
public static function getCode($code)
{
//
$url = 'https://login.windows.net/common/oauth2/token';
//
$data = array('grant_type' => 'authorization_code', 'code' => $code, 'redirect_uri' => 'redirect_uri','client_id' => 'client_id','client_secret' => 'client_secret', 'resource' => '00000002-0000-0ff1-ce00-000000000000');
//
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
//
$context = stream_context_create($options);
//
$result = json_decode(file_get_contents($url, false, $context));
//
return $result->access_token;
}
}
class EmailSearch
{
public static function getEmail($accessToken)
{
//
$url = 'https://outlook.office365.com/EWS/OData/Me';
$requestID = create_guid();
$request_headers = array('client-request-id: '. $requestID,
'return-client-request-id: true',
"Authorization: Bearer $accessToken");
//
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERAGENT, 'GV-Email-Forward/1.0');
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
var_dump($result);
}
}
function create_guid($namespace = '') {
static $guid = '';
$uid = uniqid("", true);
$data = $namespace;
$data .= $_SERVER['REQUEST_TIME'];
$data .= $_SERVER['HTTP_USER_AGENT'];
$data .= $_SERVER['LOCAL_ADDR'];
$data .= $_SERVER['LOCAL_PORT'];
$data .= $_SERVER['REMOTE_ADDR'];
$data .= $_SERVER['REMOTE_PORT'];
$hash = strtoupper(hash('ripemd128', $uid . $guid . md5($data)));
$guid = '{' .
substr($hash, 0, 8) .
'-' .
substr($hash, 8, 4) .
'-' .
substr($hash, 12, 4) .
'-' .
substr($hash, 16, 4) .
'-' .
substr($hash, 20, 12) .
'}';
return $guid;
}
您可以使用cURL:
這是一個簡單的例子:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
?>
您將在此處找到所需的選項列表: http : //www.php.net/manual/zh/function.curl-setopt.php
您可以使用PHP的cURL
函數輕松完成此操作。
例如:
<?php
$url = 'https://outlook.office365.com/EWS/OData/Me/Inbox/Messages?$top=5';
$request_headers = array('client-request-id: 9aa1c740-25e2-4841-9146-ef7018f7bf37',
'return-client-request-id: true',
"authorization: $my_access_token");
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERAGENT, 'My-Cool-WebApp/1.0');
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
// Now do something with $result...
確保對$url
的聲明使用單引號,否則$top
將被解釋為對PHP變量的引用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.