Sending custom HTTP request with PHP

自古美人都是妖i 提交于 2019-12-08 12:43:57

问题


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

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.

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}

Thanks

Associated MSDN tutorial here

Code Below:

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

回答1:


You can use cURL :

Here is a simple example :

<?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);
?>

You'll find the list of options you'll need there : http://www.php.net/manual/en/function.curl-setopt.php




回答2:


You can do this quite easily with PHP's cURL functions.

For example:

<?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...

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



来源:https://stackoverflow.com/questions/23240060/sending-custom-http-request-with-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!