doucang6739 2015-06-15 09:45
浏览 52
已采纳

XML使用curl PHP从表单发布

What is the best way to post XML from a form using Curl.

I have a HTML Form and i post the data to a new php page and all the fields are collected. How do i collect these fields in XML Format.

I can process it from a xml file, how do i alter my current codeso it doesnt use a file , but builds it on the same page then sends it.

$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: 
";
       echo $info['http_code'] . " " . $http_codes[$info['http_code']];
   }
}
echo "</br>";
var_dump($result) ;
  • 写回答

1条回答 默认 最新

  • doupian6118 2015-06-15 12:03
    关注
        $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) ;
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里