douzongluo7542 2014-12-16 21:41
浏览 43
已采纳

为什么这个请求在apikitchen中工作但在我的PHP脚本中不起作用?

I have been using apikitchen to reverse engineer an api for work and I'm unfamiliar with the curl library in php. I was wondering if someone could explain why the request I'm making returns a 401 error when I use curl in php, but it works fine on api kitchen.

My requests headers look like this after adding headers in api kitchen and hitting execute(I just added the deviceId and Authorization header in apikitchen):

Host: somesubdomain.somedomain.com
Deviceid: someID
Authorization: Token someID
User-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0

The host I am pointing to is obviously fake in the example. The real host uses GET requests to grab a company name/order number, then uses the deviceID and Authorization token to authenticate the user.

Here is the code I am using for the php script:

<?php
echo 'starting...';
$curl = curl_init('https://somesubdomain.somedomain.com/ws/orders?wildcard=0804400');

$headers = array('deviceId'=>'someID',
                    'Authorization'=>'Token someID',
                    'Host'=>'somesubdomain.somedomain.com');

$options = array(CURLOPT_RETURNTRANSFER=>1,
                    CURLOPT_VERBOSE=>1,
                    CURLOPT_USERAGENT=>'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0',
                    CURLOPT_HTTPHEADER=>$headers);

curl_setopt_array($curl, $options);
echo curl_getinfo($curl);

$response = curl_exec($curl);

if(!$response){
    echo curl_getinfo($curl);
    die('ERROR: "' . curl_error($curl) . '" - CODE: ' . curl_errno($curl));
}else{
    echo $response;
}

curl_close($curl);
echo "<br>Done!";
?>

But all i get is a 401 error:

type Status report

message

description This request requires HTTP authentication ().

Like I said before, using the same exact login credentials works in api kitchen, but when I assemble the requests using curl, it does not.

Any help is appreciated!

  • 写回答

1条回答 默认 最新

  • dscpg80066 2014-12-16 21:56
    关注

    It is probably the auth format that is incorrect. You need to do something like

    $headers = array('deviceId:someID',
                        'Authorization:Token someID',
                        'Host:somesubdomain.somedomain.com');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵