Heim  >  Artikel  >  Backend-Entwicklung  >  php post请求,该如何解决

php post请求,该如何解决

WBOY
WBOYOriginal
2016-06-13 13:24:02736Durchsuche

php post请求
在php里面怎么提交一个post请求,我要请求一个网址,传参数进去。他会返回一个xml格式的文件结果。在php里面怎么post。

------解决方案--------------------
function _xpost($url,$p)
{
$f='';
$data='';
foreach($p as $k=>$v)
{
$data.=$f.$k.'='.urlencode($v);
$f='&';
}

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$res = curl_exec($curl);

if(curl_errno($curl))
{
echo 'Curl error: ' . curl_error($curl);
}
curl_close($curl);
return $res;
}

$x=_xpost('http://abc.com/aa.php',array( 'username'=>$uname,
));
------解决方案--------------------
顶一楼 用curl
------解决方案--------------------
xml 请使用simplexml来解析.学好simplexml 以后碰到xml都不用担心了.

探讨

还有个问题请教,如果$x=_xpost('http://abc.com/aa.php',array('username'=>$uname));接收到的数据是一个xml:


1

我怎么获取之间的1这个值?

------解决方案--------------------
简单点正则之,
preg_match('/(.*?)/s',$xml,$m);
echo $m[1];
------解决方案--------------------
探讨

还有个问题请教,如果$x=_xpost('http://abc.com/aa.php',array('username'=>$uname));接收到的数据是一个xml:


1

我怎么获取之间的1这个值?

------解决方案--------------------
simplexml
------解决方案--------------------
顶6楼 正则很强大
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn