本文目录一览:
php使用cURL读取XML字节
不用那么麻烦,字符串截取即可。或者你把返回结果前自己拼接一个
?xml version="1.0" encoding="UTF-8"?
php 如何发送xml报文
?php
if( $_SERVER['REQUEST_METHOD'] === 'POST' ){
// 接收
$content = file_get_contents('php://input');
$xml = simplexml_load_string($content);
echo "来自XML接收方的响应\n";
print_r( get_object_vars($xml) );
exit;
}
// 发送行为
$xml = xml
?xml version="1.0"?
FOX
helloworld/hello
/FOX
xml;
$setting = array(
'http' = array(
'method' = 'POST',
'user_agent' = 'Client Application Name',
'header' = "Content-type: application/x-www-form-urlencoded",
'content' = $xml
)
);
$context = stream_context_create($setting);
$url = ''. $_SERVER['REQUEST_URI'];
$response = file_get_contents($url, null, $context);
echo $response;
CURL是可以的,但是参数设置比较麻烦。这种情况有一些现成的类库实现,提供一个简单的示例
请教用Curl 在php 里面模拟表单提交 文本+文件的写法
public function curlPost($url, $data, $header = null)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//将curl_exec()获取的信息以字符串返回
curl_setopt($curl, CURLOPT_TIMEOUT, 15); //只需要设置一个秒的数量就可以
curl_setopt($curl, CURLOPT_POST, 80);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1)');
if (!empty($header) isset($header)) {
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
}
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
public function get($url,$header = null)
{
$ch = curl_init();
//设置选项,包括URL
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15); //只需要设置一个秒的数量就可以
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1)');
if (!empty($header) isset($header)) {
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
}
//执行并获取HTML文档内容
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
如何用Curl 来post xml 数据
可用我的函数。publicfunctionpost($url,$post_data){$this-_ch=curl_init();curl_setopt($this-_ch,CURLOPT_USERAGENT,'Mozilla/5.0(WindowsNT6.1;rv:22.0)Gecko/20100101Firefox/22.0');curl_setopt($this-_ch,CURLOPT_FOLLOWLOCATION,TRUE);curl_setopt($this-_ch,CURLOPT_MAXREDIRS,5);curl_setopt($this-_ch,CURLOPT_HEADER,0);curl_setopt($this-_ch,CURLOPT_RETURNTRANSFER,TRUE);curl_setopt($this-_ch,CURLOPT_SSL_VERIFYPEER,false);curl_setopt($this-_ch,CURLOPT_SSL_VERIFYHOST,false);curl_setopt($this-_ch,CURLOPT_ENCODING,"");curl_setopt($this-_ch,CURLOPT_POST,TRUE);curl_setopt($this-_ch,CURLOPT_POSTFIELDS,$post_data);curl_setopt($this-_ch,CURLOPT_COOKIEFILE,getcwd().'/cookie.txt');curl_setopt($this-_ch,CURLOPT_COOKIEJAR,getcwd().'/cookie.txt');curl_setopt($this-_ch,CURLOPT_URL,$url);$this-_body=curl_exec($this-_ch);$this-_info=curl_getinfo($this-_ch);$this-_error=curl_error($this-_ch);curl_close($this-_ch);}