CURL NTML身份验证在命令行中运行,但不能与php脚本一起使用

时间:2016-09-14 07:54:40

标签: php curl

我需要调用.net网络服务。此Web服务需要NTLM身份验证。为此,我用NTLM身份验证编写了一些代码。我正在尝试使用curl与NTLM身份验证进行连接。但是从PHP脚本我得到401错误。我尝试了不同的标题内容(xml和html)。请帮忙。

以下是正在运行的命令行。

curl http://qtskofaxweb01.etch.com/TotalAgility/Services/Sdk/JobService.svc?wsdl -v --ntlm --negotiate -u kofax:Jagadish6^

以下是我的PHP脚本无效

$path = 'http://qtskofaxweb01.etch.com/TotalAgility/Services/Sdk/JobService.svc?wsdl';
$user = 'kofax';
$password = 'Jagadish6^';

$ch = curl_init($path);

$headers = array();
$headers[] = 'Accept: text/xml';
$headers[] = 'Content-Type: text/xml';

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
curl_setopt($ch, CURLOPT_USERPWD, $user.':'.$password);

$result = curl_exec($ch);

print_r(curl_getinfo($ch));
echo curl_error($ch);
curl_close($ch);
var_dump($result);

输出结果如下

Array
(
    [url] => http://qtskofaxweb01.etch.com/TotalAgility/Services/Sdk/JobService.svc?wsdl
    [content_type] => text/html
    [http_code] => 401
    [header_size] => 657
    [request_size] => 546
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 1
    [total_time] => 0.003738
    [namelookup_time] => 1.4E-5
    [connect_time] => 1.5E-5
    [pretransfer_time] => 1.7E-5
    [size_upload] => 0
    [size_download] => 1293
    [speed_download] => 345906
    [speed_upload] => 0
    [download_content_length] => 1293
    [upload_content_length] => 0
    [starttransfer_time] => 0.002228
    [redirect_time] => 0.001484
    [certinfo] => Array
        (
        )

[primary_ip] => 10.10.3.40
[redirect_url] => 

)

string '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/> <title>401 - Unauthorized: Access is denied due to invalid credentials.</title> <style type="text/css"> <!-- body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;} fieldset{padding:0 15px 10px 15px;} h1{font-size:2.4em;mar'... (length=1293)

1 个答案:

答案 0 :(得分:0)

请试试..

$url="http://qtskofaxweb01.etch.com/TotalAgility/Services/Sdk/JobService.svc?wsdl";

    $user = 'kofax';
    $password = 'Jagadish6^';

    $parameters=array();

    class Emp {
      public $user  = "";
      public $password   = "";
   }
   $e = new Emp();
   $e->user = $user ;
   $e->password   = $password ;

    $parameters['user']=$e;
    $parameters=json_encode($parameters);

    $ch = curl_init();  

    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Accept: application/json',
    'Content-type: application/x-www-form-urlencoded'
    ));
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $output=curl_exec($ch);

    if($output === false)
    {
        echo "Error Number:".curl_errno($ch)."<br>";
        echo "Error String:".curl_error($ch);
    }
    curl_close($ch);

    $obj = json_decode($output, TRUE);