PHP-POST:XML,JSON

发布:2015-06-29 08:05   点击487次   评论:0


http://blog.sina.com.cn/s/blog_43b191a901010u93.html
PHP 以POST方式提交XML、获取XML,最后解析XML

以POST方式提交XML

// Do a POST
$data="<?xml version='1.0' encoding='UTF-8'?>
<TypeRsp>
<CONNECT_ID>1</CONNECT_ID>
<MO_MESSAGE_ID>2</MO_MESSAGE_ID>
</TypeRsp>";

//$data = array('name' => 'Dennis', 'surname' => 'Pallett');

// create a new curl resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL,"http://localhost/handle_form.php");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// grab URL, and print
curl_exec($ch);



获取POST来到XML,并解析
handle_form.php
$file_in = file_get_contents("php://input"); //接收post数据

$xml = simplexml_load_string($file_in);//转换post数据为simplexml对象

foreach($xml->children() as $child)    //遍历所有节点数据
{

echo $child->getName() . ": " . $child . "<br />"; //打印节点名称和节点值

//if($child->getName()=="from")    //捡取要操作的节点
//{
//echo "i say ". ": get you!" . "<br />"; //操作节点数据
//}
}

exit;

========================================== 

http://www.cnblogs.com/caly/archive/2013/04/11/3013980.html
PHP 使用 curl 提交 json 格式数据

$data = array("name" => "Hagrid", "age" => "36");
$data_string = json_encode($data);

$ch = curl_init('http://api.local/rest/users');
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);

========================================== 

http://weixin.qq.com/q/6U2KpAblRHo99CKr_GPH

关于 GitHub 导航 部门 反馈

提示:`/home.php`入口数据仅为演示功能,不构成任何交易凭证,也不承担相关风险和责任!

Copyright © 2011-2018 xxxxx.com All rights reserved.

Run:4.169/35.540(ms); 7(sql)/2.879(MB); comm:news/detail; Upd:2024-04-16 20:01:08