dongrong5189 2016-06-12 07:49
浏览 40
已采纳

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

Here is the XHR cURL data:

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

I am using the following PHP code:

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

The first error that I got was missing streamId key. Now, I guessed this is because of "%" in the URL so I replaced them with %. After that, I got the error unauthorized access: not logged in. So, I tried to add one more option to CURLOPT. The option I added was curl_setopt($c, CURLOPT_AUTHORIZATION, 'OAuth veryLargeRandomKey:example');. I had no reason to believe that it will work and it didn't. The error I got was

Use of undefined constant CURLOPT_AUTHORIZATION - assumed 'CURLOPT_AUTHORIZATION'

I have removed this extra option now. My question is how can I successfully authenticate my request. If you need any other information please let me know :)

  • 写回答

1条回答 默认 最新

  • duancuo1234 2016-06-12 17:53
    关注

    You need to know which of those headers are actually required for authentication. And then add them like this:

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

    CURLOPT_AUTHORIZATION constant doesn't exist, for the list of usable constants see curl_setopt.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测