2016-06-12 100 views
1

如何进行身份验证请求,下面是XHR卷曲数据:使用卷曲在PHP

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。现在,我猜这是因为URL中的"%",所以我用%替换了它们。之后,我得到了错误unauthorized access: not logged in。 所以,我试图增加一个选项CURLOPT。我添加的选项是curl_setopt($c, CURLOPT_AUTHORIZATION, 'OAuth veryLargeRandomKey:example');。我没有理由相信它会起作用,但事实并非如此。我得到的错误是

Use of undefined constant CURLOPT_AUTHORIZATION - assumed 'CURLOPT_AUTHORIZATION'

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

回答

0

您需要知道哪些头实际需要进行身份验证。然后添加他们是这样的:

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

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

+0

非常感谢你,哥们:)。 我认为每个选项都有自己的'CURLOPT'常量。我是否也可以在HTTPHEADER常量中放置所有标头,如'useragent'等,还是需要使用它们自己的常量标头? –

+0

是的,你可以,但使用它们的专用常量会使代码更清晰(在我看来)。不用谢) –