且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用PHP发送自定义HTTP请求

更新时间:2022-06-26 09:43:25

使用例如:

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