使用PHP发送自定义HTTP请求


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}

感谢

此处为相关MSDN教程

以下代码:

$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/en/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变量的引用。