XML使用curl PHP从表单发布

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

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

我可以从xml文件处理它,如何更改我当前的代码,因为它不使用文件,但是在同一页面上构建它 然后发送它。</ p>

  $ filename =“data.xml”; 
$ handle = fopen($ filename,“r”);
$ XPost = fread($ handle,filesize($ filename));
nclclose($ handle);

$ url =“http://test.com/webservicerequest.asmx";
nnchch = curl_init(); //初始化curl句柄

curl_setopt($ ch,CURLOPT_VERBOSE,1); //设置url发布到
curl_setopt($ ch,CURLOPT_URL,$ url); //设置url发布到
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1); //返回变量
curl_setopt($ ch,CURLOPT_HTTPHEADER,Array(“Content-Type:text / xml”));

ncurl_setopt($ ch,CURLOPT_HEADER,“http://test.com/webservicerequest/SubmitLead “);
curl_setopt($ ch,CURLOPT_TIMEOUT,99999999); //在4s
curl_setopt之后超时($ ch,CURLOPT_POSTFIELDS,$ XPost); //添加POST字段
curl_setopt($ ch,CURLOPT_POST,1);

$ result = curl_exec($ ch); //运行整个过程

if(空($ result)){
//发生某种错误
die(curl_error($ ch));
curl_close($ ch); //关闭cURL处理程序
} else {
$ info = curl_getinfo($ ch);
curl_close($ ch); //关闭cURL处理程序

if(empty($ info ['http_code'])){
die(“没有返回HTTP代码”);
} else {
//加载HTTP代码\ n $ http_codes = parse_ini_file(“response.inc”);

// echo echo
echo“服务器响应:
”;
echo $ info ['http_code']。 “”。 $ http_codes [$ info ['http_code']];
}
}
echo“&lt; / br&gt;”;
var_dump($ result);
</ code> </ pre>
</ div >

展开原文

原文

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个回答



  $ data = array(
“line1”=&gt;“sample data”,
“line2”=&gt; ;“示例数据2”,
);

$ data_string = json_encode($ data);
$ url =“http://test.com/webservicerequest.asmx";
nn $ ch = curl_init();
curl_setopt($ ch,CURLOPT_URL,$ url); //设置url发布到
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);
?&gt;
</ code> </ pre>
</ div>

展开原文

原文

    $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) ;
?>

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐