1

我用Curl用PHP编写了一个小程序,它在curl 7.21版本上运行良好。我已经更新了我的 PHP,现在使用 curl 7.26。不幸的是,现在我正在尝试访问回复的服务器(标题):HTTP/1.1 501 Method POST 未在 RFC 2068 中定义,并且不受 Servlet API 支持。之前,使用 Curl 7.21,它回复(标题)OK,一切正常。

请帮忙,因为这让我发疯:(...

这是访问其他服务器的代码:

function authenticateihc($username,$password,$ihcip,$timeout,$debug=0)
{
 if (cURLcheckBasicFunctions())
 {
   // Definition of XML request according to IHC standards
   $xml_request  = "<?xml version='1.0' encoding='UTF-8'?>\n";
   $xml_request .= "<SOAP-ENV:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\n";
   $xml_request .= "<SOAP-ENV:Body>\n";
   $xml_request .= "<ns1:authenticate1 xmlns:ns1=\"utcs\" xsi:type=\"ns1:WSAuthenticationData\">\n"; 
   $xml_request .= "<ns1:password xsi:type=\"xsd:string\">".$password."</ns1:password>\n";
   $xml_request .= "<ns1:username xsi:type=\"xsd:string\">".$username."</ns1:username>\n";  
   $xml_request .= "<ns1:application xsi:type=\"xsd:string\">treeview</ns1:application>\n";
   $xml_request .= "</ns1:authenticate1>\n";
   $xml_request .= "</SOAP-ENV:Body>\n";
   $xml_request .= "</SOAP-ENV:Envelope>\n";

   $ch = curl_init();
   curl_setopt($ch, CURLOPT_FRESH_CONNECT, true); 
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
   curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_request);
   // Forcer cURL à utiliser un nouveau cookie de session 
   curl_setopt($ch, CURLOPT_COOKIESESSION, true); 
   curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); 
   curl_setopt($ch, CURLOPT_URL,$ihcip.'/ws/AuthenticationService');
   curl_setopt($ch, CURLOPT_SSLVERSION, 3);
   curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); 
   curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 

   // Si l'URL est en HTTPS 
   if (preg_match('`^https://`i', $ihcip))
   { 
    // Ne pas vérifier la validité du certificat SSL 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
   } 

   if ($debug == 1) {
      curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'dbg_curl_data');
      curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'dbg_curl_data');
      curl_setopt($ch, CURLINFO_HEADER_OUT, true);
   }

   $result = curl_exec($ch);
   $info = curl_getinfo($ch);


   if ($debug == 1) 
   {
      echo '<fieldset><legend>request headers</legend> <pre>', htmlspecialchars(curl_getinfo($ch, CURLINFO_HEADER_OUT)), '</pre> </fieldset>';
      echo '<fieldset><legend>xml request</legend> <pre>', htmlspecialchars($xml_request), '</pre> </fieldset>';
      echo '<fieldset><legend>response</legend> <pre>', htmlspecialchars(dbg_curl_data(null)), '</pre> </fieldset>';
      echo '<fieldset><legend>curl info</legend> <pre>error: ' . curl_error($ch).'<BR>';
      echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'].'<BR></pre> </fieldset>';
   }

   if ($debug == 2) 
   {
       echo '<fieldset><legend>Variable</legend> <pre>', htmlspecialchars($result), '</pre> </fieldset>';
   }

   $result_from_xml = array (
    "loginSuccessful" => get_string_between($result, '<ns1:loginWasSuccessful xsi:type="xsd:boolean">' , '</ns1:loginWasSuccessful'),
    "AccountInvalid" => get_string_between($result, '<ns1:loginFailedDueToAccountInvalid xsi:type="xsd:boolean">' , '</ns1:loginFailedDueToAccountInvalid'),
    "InsufficientRights" => get_string_between($result, '<ns1:loginFailedDueToInsufficientUserRights xsi:type="xsd:boolean">' , '</ns1:loginFailedDueToInsufficientUserRights')
   );

   curl_close($ch);
   return $result_from_xml;
 }
 else 
 {
     echo "Error<br>";
     echo "_____<br>";
     echo "Curl installed on this computer: ".cURLcheckBasicFunctions()."<br>";
 }
}

function cURLcheckBasicFunctions() 
{ 
  if( !function_exists("curl_init") && 
      !function_exists("curl_setopt") && 
      !function_exists("curl_exec") && 
      !function_exists("curl_close") ) return false; 
  else return true; 
} 

function dbg_curl_data($curl, $data=null) {
  static $buffer = '';

  if ( is_null($curl) ) {
    $r = $buffer;
    $buffer = '';
    return $r;
  }
  else {
    $buffer .= $data;
    return strlen($data);
  }
}

function get_string_between($string, $start, $end){
    $string = " ".$string;
    $ini = strpos($string,$start);
    if ($ini == 0) return "";
    $ini += strlen($start);   
    $len = strpos($string,$end,$ini) - $ini;
    return substr($string,$ini,$len);
}
4

0 回答 0