2012-07-31 111 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))); 

?>