如何在PHP中使用cURL验证请求

时间:2016-06-12 07:49:45

标签: php curl

以下是XHR cURL数据:

curl "http://example.com/stream/contents?streamId=user"%"2FmyId"%"2Fcategory"%"2Fapple&count=40&ranked=newest" -H
     "Cookie: large-cookie-data" -H
     "$Authorization.example: $ExampleAuth" -H
     "Accept-Encoding: gzip, deflate, sdch" -H
     "Accept-Language: en-US,en;q=0.8" -H
     "Authorization: OAuth veryLargeRandomKey:example" -H
     "Content-Type: application/json" -H
     "Accept: */*" -H
     "Referer: http://example.com/somepage" -H
     "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36" -H
     "Connection: keep-alive" --compressed

我正在使用以下PHP代码:

$c = curl_init('http://example.com/streams/contents?streamId=user"%"2F3ad1e06d-e1ac-49b4-ae99-21f96b2af86f"%"2Fcategory"%"2Fapple&count=40&ranked=newest');                 
curl_setopt($c, CURLOPT_USERAGENT, "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0");
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);         
curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);     
curl_setopt($c, CURLOPT_COOKIE, 'Big cookie string here');
curl_setopt($c, CURLOPT_REFERER, 'http://example.com/page/');
$z = curl_getinfo($c);
$s = curl_exec($c);
curl_close($c);  

我得到的第一个错误是missing streamId key。现在,我猜测这是因为网址中的"%"因此我将其替换为%。之后,我收到错误unauthorized access: not logged in。 所以,我尝试向CURLOPT添加一个选项。我添加的选项是curl_setopt($c, CURLOPT_AUTHORIZATION, 'OAuth veryLargeRandomKey:example');。我没有理由相信它会起作用但事实并非如此。我得到的错误是

Use of undefined constant CURLOPT_AUTHORIZATION - assumed 'CURLOPT_AUTHORIZATION'

我现在已经删除了这个额外选项。我的问题是如何成功验证我的请求。如果您需要任何其他信息,请告诉我:))

1 个答案:

答案 0 :(得分:0)

您需要知道哪些标头实际上是验证所必需的。然后像这样添加它们:

curl_setopt($c, CURLOPT_HTTPHEADER, array(
    '$Authorization.example: $ExampleAuth',
    'Authorization: OAuth veryLargeRandomKey:example'
));

CURLOPT_AUTHORIZATION常量不存在,可用常量列表请参见curl_setopt