2011-10-13 69 views
0

我一直在嘗試從PHP發送xml消息到asp,並使用CURL輸出對我的php頁面的響應,但沒有收到任何迴應。這是我曾嘗試過的:來自asp頁面的XML響應

<?php 
$url = "https://someweb.asp"; 
$post_string = "xmlmessage=<?xml version='1.0' encoding='UTF-8'?> 
<abc> 
<UserId>123</UserId> 
</abc>"; 

//$header = "POST HTTPS/1.0 \r\n"; 
$header = "Content-type: text/xml \r\n"; 
$header .= "Content-length: ".strlen($post_string)." \r\n"; 
$header .= "Content-transfer-encoding: text \r\n"; 
$header .= "Connection: close \r\n\r\n"; 
$header .= $post_string; 

$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_TIMEOUT, 4); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header); 

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

if ($output == false || $info['http_code'] != 200) { 
    $output = "No cURL data returned for $url [". $info['http_code']. "]"; 
    if (curl_error($ch)) 
    $output .= "\n". curl_error($ch); 
    } 
else 
    {curl_close($ch);} 

echo $output; 
?> 

任何人都可以請指導我,我錯了嗎?

回答

0

不要建立一個簡單的POST自定義請求。 CURL完全有能力在沒有所有這些詭計的情況下發帖:

$xml = <<<EOL 
<?xml version='1.0' encoding='UTF-8'?> 
<abc> 
<UserId>123</UserId> 
</abc> 
EOL; 

$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, array('xmlmessage' => $xml)); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_TIMEOUT, 4); 

$result = curl_exec($ch); 
if ($result === FALSE) { 
    die(curl_error($ch)); 
} 

echo $result 
+0

我試過你的代碼時得到這個:Warning:curl_setopt()[function.curl-setopt]:無效的curl配置選項第25行中的/home/add.php 錯誤的請求(無效的數字) – user994144

+0

第25行:curl_setopt($ ch,CURLOPT_POST_FIELDS,array('xmlmessage'=> $ xml)); – user994144

+0

抱歉,我的錯字。應該是CURLOPT_POSTFIELDS(只有一個'_') –

0
$post_string = "xmlmessage=<?xml version='1.0' encoding='UTF-8'?> 
<abc> 
<UserId>123</UserId> 
</abc>"; 

交換雙和單引號

$post_string = 'xmlmessage=<?xml version="1.0" encoding="UTF-8"?> 
<abc> 
<UserId>123</UserId> 
</abc>'; 

單引號是無效的XML標記

+0

沒有幫助。我收到錯誤:「〜No xmlmessage = parameter was provided」when I do this:

user994144