标题卷曲问题

时间:2012-10-16 14:04:55

标签: php curl http-headers libcurl

我在CURL上遇到问题,我的脚本有两部分,第一部分是autentication,第二部分是请求,第一部分工作正常,我得到结果代码和一个id,在第二部分部分我设置了带有该ID的标头并将其发送到标头上,但我收到此错误(< HTTP / 1.1 401 Unauthorized),但是,我现在尝试使用fireon的插件(ModifyHeaders),我现在得到响应(我在这个软件上设置了id并通过URL访问了WS),我不知道发生了什么或者我该如何解决它, 这是代码:

$postargs = array('username' => 'user', 'password' => 'pass', 'key' => '1234567890');
$postargs = json_encode($postargs);

$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json; charset=utf-8","Accept:application/json, text/javascript, */*; q=0.01"));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postargs);
curl_setopt($ch, CURLOPT_URL, "https://ws.com/login");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);

curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
$fh = fopen('curl_debug.txt', 'w');
curl_setopt($ch, CURLOPT_STDERR, $fh);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);            
$response = curl_exec($ch);
$a = json_decode($response);
echo $a->result;
curl_close($ch);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://ws.com/data");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("id : $a->result","dataRequest : name","Content-Type: application/json; charset=utf-8","Accept:application/json, text/javascript, */*; q=0.01"));
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);
$fh2 = fopen('curl_debug2.txt', 'w');
curl_setopt($ch, CURLOPT_STDERR, $fh2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
$response2 = curl_exec($ch); 

脚本有什么问题?为什么标题(在这种情况下是id)不是由脚本进行,而是由ModifyHeaders进行?

提前致谢。

1 个答案:

答案 0 :(得分:1)

在您关闭经过身份验证的curl连接并初始化一个新连接后,第二个请求是GET请求。这是一个完全没有身份验证的请求。您没有像在POST中那样传递身份验证数组。

看看这个:Can I make more then one request on a curl connection?