2016-06-12 98 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

是的,你可以,但使用它們的專用常量會使代碼更清晰(在我看來)。不用謝) –