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

为什么这个请求在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!

我一直在使用 apikitchen 对工作的api进行逆向工程,我不熟悉php中的curl库。 我想知道是否有人可以解释为什么我正在制作的请求在php中使用curl时返回401错误,但它在api kitchen上工作正常。 p>

在api kitchen中添加标题并点击执行后,我的请求标题如下所示(我刚刚在apikitchen中添加了deviceId和Authorization标题): p>

  主机:somesubdomain.somedomain.com 
Deviceid:someID 
授权:Token someID 
User-agent:Mozilla / 5.0(Windows NT 6.1; WOW64; rv:34.0)Gecko / 20100101 Firefox / 34.0 
  code>   pre> 
 
 

我指向的主机在示例中显然是假的。 真实主机使用GET请求获取公司名称/订单号,然后使用deviceID和授权令牌对用户进行身份验证。 p>

以下是我用于php脚本的代码: p>

 &lt;?php 
echo'starting ...';  
 $ curl = curl_init('https://somesubdomain.somedomain.com/ws/orders?wildcard=0804400'); 
 
 $ headers = array('deviceId'=&gt;'someID',
' 授权'=&gt;'令牌someID',
'主机'=&gt;'somesubdomain.somedomain.com'); 
 
 $ options = array(CURLOPT_RETURNTRANSFER =&gt; 1,
 CURLOPT_VERBOSE =&gt; 1,  
 CURLOPT_USERAGENT =&gt;'Mozilla / 5.0(Windows NT 6.1; WOW64; rv:34.0)Gecko / 20100101 Firefox / 34.0',
 CURLOPT_HTTPHEADER =&gt; $ headers); 
 
 ncurl_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; 
} 
 
 ncurl_close($ curl); 
echo”&lt; br&gt;  ;完成!“; 
?&gt; 
  code>  pre> 
 
 

但我得到的是401错误: p> 类型状态报告 message description此请求需要HTTP身份验证()。 code> pre>

就像我之前说的那样,使用相同的 登录凭证在api kitchen中工作,但是当我使用curl汇编请求时,它没有。 p>

感谢任何帮助! p> div>

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