2012-07-31 84 views
2

我想通过POST方法发送一个XML到CURL到认证的webServices,但由于某种原因服务器拒绝了XML文件,我给了我一个错误500,但是另一个WebService比通过GET方法,没有问题。以下代码是我正在尝试。通过CURL发送XML文件到WebServices时出错

<?php 
    $request_xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?> 
    <FiltroLicitaciones xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"> 
    <CantidadRegistro>10</CantidadRegistro> 
    <Texto>memo</Texto> 
    <CodigoRegion xsi:nil=\"true\" /> 
    <CodigoEstado>1</CodigoEstado> 
    <TipoFecha>FechaPublicacion</TipoFecha> 
    <FechaDesde>2011-06-01T00:00:00</FechaDesde> 
    <FechaHasta>2011-08-01T00:00:00</FechaHasta> 
    </FiltroLicitaciones>"; 

    //Initialize handle and set options 
    $username = 'user'; 
    $password = 'pass'; 
    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_URL, 'https://www.mercadopublico.cl/movil/licitaciones/porFecha'); 
//curl_setopt($ch, CURLOPT_URL, "http://localhost/server.php"); 
    curl_setopt($ch, CURLOPT_USERPWD, $username.':'.$password); 
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_HEADER, 1); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 4); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $request_xml); 

    $result = curl_exec($ch); 

    curl_close($ch); 

    print_r($result); 

    ?> 

我不知道是否将是XML格式不正确的一个错误,但服务器返回我的随机500错误与下面的头:

HTTP/1.1 500 The server encountered an error processing the request. Please see the  server logs for more details. 
Cache-Control: private 
Content-Length: 1047 
Content-Type: text/html 
Server: Microsoft-IIS/7.5 
X-AspNet-Version: 4.0.30319 
X-Powered-By: ASP.NET 
Date: Mon, 30 Jul 2012 23:53:05 GMT 

我在本地服务器做了一个测试,对于看到的数据来了,结果以下是这样的:

HTTP/1.1 200 OK 
Date: Mon, 30 Jul 2012 23:41:50 GMT 
Server: Apache/2.2.22 (Fedora) 
X-Powered-By: PHP/5.3.14 
Content-Length: 2450 
Connection: close 
Content-Type: text/html; charset=UTF-8 

Array 
( 
[GLOBALS] => Array 
*RECURSION* 
    [_POST] => Array 
    ( 
     [<?xml version] => "1.0" encoding="utf-8" ?> 
     <FiltroLicitaciones xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
     <CantidadRegistro>10</CantidadRegistro> 
     <Texto>memo</Texto> 
     <CodigoRegion xsi:nil="true" /> 
     <CodigoEstado>1</CodigoEstado> 
     <TipoFecha>FechaPublicacion</TipoFecha> 
     <FechaDesde>2011-06-01T00:00:00</FechaDesde> 
     <FechaHasta>2011-08-01T00:00:00</FechaHasta> 
     </FiltroLicitaciones> 
    ) 

[_GET] => Array 
    ( 
    ) 

[_COOKIE] => Array 
    ( 
    ) 

[_FILES] => Array 
    ( 
    ) 

[_ENV] => Array 
    ( 
    ) 

[_REQUEST] => Array 
    ( 
     [<?xml version] => "1.0" encoding="utf-8" ?> 
     <FiltroLicitaciones xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
     <CantidadRegistro>10</CantidadRegistro> 
     <Texto>memo</Texto> 
     <CodigoRegion xsi:nil="true" /> 
     <CodigoEstado>1</CodigoEstado> 
     <TipoFecha>FechaPublicacion</TipoFecha> 
     <FechaDesde>2011-06-01T00:00:00</FechaDesde> 
     <FechaHasta>2011-08-01T00:00:00</FechaHasta> 
     </FiltroLicitaciones> 
    ) 

[_SERVER] => Array 
    ( 
     [HTTP_HOST] => localhost 
     [HTTP_ACCEPT] => */* 
     [CONTENT_LENGTH] => 469 
     [CONTENT_TYPE] => application/x-www-form-urlencoded 
     [PATH] => /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin 
     [SERVER_SIGNATURE] => <address>Apache/2.2.22 (Fedora) Server at localhost Port 80</address> 

     [SERVER_SOFTWARE] => Apache/2.2.22 (Fedora) 
     [SERVER_NAME] => localhost 
     [SERVER_ADDR] => ::1 
     [SERVER_PORT] => 80 
     [REMOTE_ADDR] => ::1 
     [DOCUMENT_ROOT] => /var/www/html 
     [SERVER_ADMIN] => [email protected] 
     [SCRIPT_FILENAME] => /var/www/html/server.php 
     [REMOTE_PORT] => 37712 
     [GATEWAY_INTERFACE] => CGI/1.1 
     [SERVER_PROTOCOL] => HTTP/1.1 
     [REQUEST_METHOD] => POST 
     [QUERY_STRING] => 
     [REQUEST_URI] => /server.php 
     [SCRIPT_NAME] => /server.php 
     [PHP_SELF] => /server.php 
     [PHP_AUTH_USER] => user 
     [PHP_AUTH_PW] => pass 
     [REQUEST_TIME] => 1343691710 
    ) 

我希望你能HEL我。

谢谢。

最好的问候。

回答

0

嗯,我不知道,如果500错误是XML,但正如我看到你的代码中的XML有没有url变量:

myxml=<xml></xml> 

因为你可以从响应的URL看参数已成为

[<?xml version] => "1.0" encoding="utf-8" ?> 

并且这是错误的。

如果没有指定的API文档的任何URL参数,那么你或许应该设置正确的卷曲头:

curl_setopt($ch,CURLOPT_HTTPHEADER, Array("Content-Type: application/xml"); 

像这个服务器知道期望

+0

谢谢,这对我有效:D – 2012-07-31 17:17:29

0

注意你的样品倾倒的这一部分:

[_POST] => Array 
    ( 
     [<?xml version] 

你的XML数据解释为表格形式的数据上传,与<?xml version一个字段名。这破坏了你的XML结构,导致你提交给它的服务器出现分析错误。

+0

好的,谢谢,我怎么能删除这个:[<? Xml版本]并替换为<?在将XML发送到服务器之前的Xml版本。 问候 – 2012-07-31 17:09:41

0

阅读下面的网址是什么第一,我认为这是非常使用全给你

发送XML文件到一个web服务(使用卷曲)

http://softwaredevelopmentindia.wordpress.com/2007/07/09/sending-xml-files-to-a-webservice-using-curl/

或试试这个

调用Web服务。非常有趣!! ...当它工作。最大的挑战之一是发送XML文档并获取响应,尤其是XML文档。我想出了一个PHP函数,它隐藏了用户的所有必要逻辑,并处理XML文档的发布并返回服务器响应的任何内容。它依赖于PHP的cURL库(所以你需要在你的服务器上正确配置才能工作)。您只需创建XML文档,选择要将XML文档发布到的URL(和端口),然后使用该功能处理其余部分。以下是功能代码。正如您所看到的,该函数可以处理启用了SSL的服务器,因为许多Web服务都运行在HTTPS上,所以它提供了很大的优势。

//打开一个HTTP通道,传输数据和返回接收缓冲器

function xml_post($post_xml, $url, $port) 
{ 
$user_agent = $_SERVER[’HTTP_USER_AGENT’]; 
$ch = curl_init(); // initialize curl handle 
curl_setopt($ch, CURLOPT_URL, $url); // set url to post to 
curl_setopt($ch, CURLOPT_FAILONERROR, 1); // Fail on errors 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // allow redirects 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable 
curl_setopt($ch, CURLOPT_PORT, $port); //Set the port number 
curl_setopt($ch, CURLOPT_TIMEOUT, 15); // times out after 15s 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_xml); // add POST fields 
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); 
if($port==443) 
{ 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
} 
$data = curl_exec($ch); 
curl_close($ch); 
return $data; 
} 

下面的例子示出了功能的工作原理,通过张贴的形式的XML文档

<?xml version=”1.0″ encoding=”iso-8859-1″?> 

<Document> 

    <Message> 

    Your Name 

    </Message> 

</Document> 

到“侦听器”脚本,它接受XML文档并返回一个回复(另一个XML文档)。在这种情况下,听众很简单。它所做的只是用“Reply”替换“Message”标签并打印生成的XML。当然,听众可以做各种事情来响应POST。

<?php 

    if (!isset($HTTP_RAW_POST_DATA)) 

    { 

     $HTTP_RAW_POST_DATA = file_get_contents(‘php://input’); 

    } 

    $xml = str_replace(”Message”,”Reply” , $HTTP_RAW_POST_DATA); 

    print((trim($xml))); 

?>