繁体   English   中英

使用curl PHP从表单发送XML Post

[英]XML Post from form using curl PHP

使用Curl从表单发布XML的最佳方法是什么。

我有一个HTML表单,我将数据发布到一个新的php页面,并收集了所有字段。 如何收集XML格式的这些字段。

我可以从xml文件处理它,如何更改当前代码,所以它不使用文件,而是在同一页面上构建它,然后发送它。

$filename = "data.xml";
$handle = fopen($filename, "r");
$XPost = fread($handle, filesize($filename));
fclose($handle);

$url = "http://test.com/webservicerequest.asmx";

$ch = curl_init(); // initialize curl handle

curl_setopt($ch, CURLOPT_VERBOSE, 1); // set url to post to
curl_setopt($ch, CURLOPT_URL, $url); // set url to post to
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return into a variable
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));

curl_setopt($ch, CURLOPT_HEADER, "http://test.com/webservicerequest/SubmitLead");
curl_setopt($ch, CURLOPT_TIMEOUT, 99999999); // times out after 4s
curl_setopt($ch, CURLOPT_POSTFIELDS, $XPost); // add POST fields
curl_setopt($ch, CURLOPT_POST, 1);

$result = curl_exec($ch); // run the whole process

if (empty($result)) {
   // some kind of an error happened
   die(curl_error($ch));
   curl_close($ch); // close cURL handler
} else {
   $info = curl_getinfo($ch);
   curl_close($ch); // close cURL handler

   if (empty($info['http_code'])) {
           die("No HTTP code was returned");
   } else {
       // load the HTTP codes
       $http_codes = parse_ini_file("response.inc");

       // echo results
       echo "The server responded: \n";
       echo $info['http_code'] . " " . $http_codes[$info['http_code']];
   }
}
echo "</br>";
var_dump($result) ;
    $data = array(
        "line1" => "sample data",
         "line2" => "sample data 2",
        );

$data_string = json_encode($data);
    $url = "http://test.com/webservicerequest.asmx";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url); // set url to post to
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($data_string))
    );

    $result = curl_exec($ch);
var_dump($result) ;
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM