我是cURL的新手。我正在尝试发送XML请求并将其作为XML的响应发送到远程服务器中的其他Web应用程序。 以下是我要发送的代码:
<?php
//header("refresh:5;url=form.html");
if(isset($_POST['create_xml'])){
$contact = "contact";
$first_name = $_POST["element_1"];
$last_name = $_POST["element_2"];
$email = $_POST["element_3"];
$country_code=$_POST["element_4_1"];
$contact_number=$_POST["element_4_2"].$_POST["element_4_3"];
$comments = $_POST["element_5"];
//if ($first_name && $last_name && $email && $contact_number && $comments) {
//echo "Thank you for submitting your form. You may submit email service requests to our Support Center at:";
//} else {
//exit("You have not filled out all the required fields. Place hit your back button and fill out all the required fields.");
//}
$xml = '<?xml version="1.0" encoding="UTF-8"?>';
$xml .= "<command>";
$xml .= "ADD_NEW_CONTACT";
$xml .= "</command>";
$xml .= "<data>";
$xml .= "<name>";
$xml .= $first_name.''.$last_name;
$xml .= "</name>";
$xml .= "<username>";
$xml .= $email;
$xml .= "</username>";
$xml .= "<preferredemail>";
$xml .= $email;
$xml .= "</preferredemail>";
$xml .= "<mobile>";
$xml .= "<countrycode>";
$xml .= $country_code;
$xml .= "</countrycode>";
$xml .= "<mobilenumber>";
$xml .= $contact_number;
$xml .= "</mobilenumber>";
$xml .= "</mobile>";
$xml .= "<gender>";
$xml .= "TBD";
$xml .= "</gender>";
$xml .= "</data>";
$xml .= "</groupzsyncreq>";
$xml =htmlentities($xml);
//echo $xml;
/**
* Define POST URL and also payload
*/
define('XML_POST_URL', 'http://www.testapp.com/test?request=');
/**
* Initialize handle and set options
*/
$ch = curl_init();
set_time_limit(0);
curl_setopt($ch, CURLOPT_URL, XML_POST_URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close'));
/**
* Execute the request and also time the transaction
*/
$start = array_sum(explode(' ', microtime()));
$result = curl_exec($ch);
$stop = array_sum(explode(' ', microtime()));
$totalTime = $stop - $start;
/**
* Check for errors
*/
if ( curl_errno($ch) ) {
$result = 'ERROR -> ' . curl_errno($ch) . ': ' . curl_error($ch);
} else {
$returnCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
switch($returnCode){
case 404:
$result = 'ERROR -> 404 Not Found';
break;
default:
break;
}
}
/**
* Close the handle
*/
curl_close($ch);
/**
* Output the results and time
*/
echo 'Total time for request: ' . $totalTime . "\n";
echo $result;
/**
* Exit the script
*/
exit(0);
}
?>
现在,当我尝试从本地系统发送XML请求时,我收到此错误
Total time for request: 20.308043956757 ERROR -> 6: Couldn't resolve host 'www.testapp.com'. But, `www.testapp.com` is fine and is up. How to solve this error.
答案 0 :(得分:0)
define('XML_POST_URL', 'http://www.testapp.com/test?request=');
我认为,这还不完整。 你能否在wsdl结束时检查服务标签下的终点网址。
答案 1 :(得分:0)
在使用xml
时更改此http标头curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
也可以使用此curl选项进行http POST
curl_setopt($ch, CURLOPT_POST, true);