首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CURLAUTH_NEGOTIATE在PHP中的区别。-在cURL 7.57.0进行谈判

CURLAUTH_NEGOTIATE在PHP中的区别。-在cURL 7.57.0进行谈判
EN

Stack Overflow用户
提问于 2017-12-20 00:06:36
回答 1查看 1.6K关注 0票数 0

有人能说明PHP中的CURLAUTH_NEGOTIATE (PHP5.6.4与libcurlv7.39.0一起运行)和--在cURL 7.57.0中协商的不同之处吗?

通过命令行使用cURL 7.57.0进行以下请求:

代码语言:javascript
复制
curl --negotiate -u MYDOMAIN\myuser:mypass  http://myip:myport/mywebservice.svc

当我使用libcurlv7.39.0使用PHP5.6.4(运行在上)尝试下面的代码时,远程web服务器返回一个401.2错误:

代码语言:javascript
复制
<?php
//Set vars
$url = "http://myip:myport/mywebservice.svc"; // asmx URL of WSDL
$un = "myuser";  //  username
$password = "mypass"; // password
$domain = "MYDOMAIN\\"; // domain

//Create, Send and Output cURL Request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);    
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NEGOTIATE); 
curl_setopt($ch, CURLOPT_USERPWD,  $domain . $un.":".$password);

$response = curl_exec($ch); 
echo $response;
?>

这里是PHP:

代码语言:javascript
复制
cURL support => enabled
cURL Information => 7.39.0
Age => 3
**Features**
AsynchDNS => Yes
CharConv => No
Debug => No
GSS-Negotiate => No
IDN => Yes
IPv6 => Yes
krb4 => No
Largefile => Yes
libz => Yes
NTLM => Yes
NTLMWB => No
SPNEGO => Yes
SSL => Yes
SSPI => Yes
TLS-SRP => No
Protocols => dict, file, ftp, ftps, gopher, http, https, imap, imaps, ldap, pop3, pop3s, rtsp, scp, sftp, smtp, smtps, telnet, tftp
Host => i386-pc-win32
SSL Version OpenSSL/1.0.1i
ZLib Version => 1.2.7.3
libSSH Version => libssh2/1.4.3

任何见解都是非常感谢的!谢谢丹

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-20 02:14:52

为了解决上面提到的问题,我只是绕过PHP使用的libcurl。我从cURL下载了https://curl.haxx.se/download.html的最新版本(我们的服务器在windows上,只是FYI),并将其放在一个可访问的目录中。

然后简单地编写以下代码:

代码语言:javascript
复制
<?php

//Set vars
$url = "http://<my_host_ip_or_domain>:<my_host_post_if_needed>/<my_path_etc>";
$userName = "DOMAIN\username";
$password = "password";
$filePath = "myxmlfile.xml";
$contentLength = strlen(file_get_contents($filePath));

//Set any curl args (other than headers) you wish - per https://curl.haxx.se/docs/manpage.html (These are the ones I needed to solve my particular authentication problem)
$curlSettings = array();
$curlSettings["--negotiate"] = '';
$curlSettings["-u"] = $userName .':'.  $password;
$curlSettings["--http1.1"] = '';
$curlSettings["--location-trusted"] = '';
$curlSettings["--post301"] = '';
$curlSettings["--data"] = '@"'. $filePath .'"';


//Set any cURL headers you want
$curlHeaders = array(
    'Content-Type: text/xml; charset=utf-8',
    'Content-Length: '. $contentLength,
    'SOAPAction: http://tempuri.org/blahblahblah',
    'Host: <my_host_ip>:<my_host_port>'
);

//Call the function
$curlResp = curlRequest($url, $curlSettings, $curlHeaders);

//Output the result
echo '<pre>';
print_r($curlResp);
echo '</pre>';


//Handle cURL Request
function curlRequest($url,$args,$headers)
{   
    /*
        Note: This needs to be the full path to CURL on server or you can set it to run as a windows cmd (as I have), 
        by following the instructions outlined by Michiel van Oosterhout in the following post: 
        https://stackoverflow.com/questions/9507353/how-do-i-install-set-up-and-use-curl-on-windows 
    */
    $strCurl = "curl";

    //Add Args / Setting to cURL request
    foreach ($args as $key => $val)
    {
        $strCurl .= " ". $key ." ". $val;
    }

    //Add headers to cURL request
    foreach ($headers as $h)
    {
        $strCurl .= ' --header "'. $h .'"';
    }

    //Set the URL
    $strCurl .= " ". $url;

    //Execute the curl request
    exec($strCurl, $resp);

    //Return an array of the response
    return $resp;
}

?>

就像一种魅力!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47897012

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档