使用PHP发送自定义HTTP请求 [英] Sending custom HTTP request with PHP

查看:108
本文介绍了使用PHP发送自定义HTTP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用新的Office 365 API进行身份验证.我已经成功通过身份验证,并且正在玫瑰花园里游泳.

I am trying to authenticate with the new Office 365 API. I have successfully authenticated and I'm swimming in a garden of roses.

但是,最后一部分是发送带有我闪亮的新访问令牌的自定义HTTP请求.不过,它似乎没有任何作用.

However, the last part is to sent a custom HTTP request with my shiny new access token. It doesn't seem to be working though.

有人可以概述一下如何使用PHP发送下面详细介绍的http请求吗?

Can someone please outline how to send the http request detailed below with PHP?

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;
}

推荐答案

使用例如:

<?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变量的引用.

Make sure you use single quotes for the declaration of $url, otherwise $top will be interpreted as a reference to a PHP variable.

这篇关于使用PHP发送自定义HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆