//php 的json http的请求
function http_post_data($url,$data_string)
{
$ch = curl_init();// 创建一个新cURL资源
curl_setopt($ch, CURLOPT_POST, 1);//设置请求为post;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);// 添加post数据到请求中
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//支持https请求
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//curl获取页面内容或提交数据,作为变量储存,而不是直接输出。
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json; charset=utf-8',
'Content-Length: ' . strlen($data_string)));
$return_content=curl_exec($ch);// 抓取URL并把它传递给浏览器
curl_close($ch);//关闭cURL资源,并且释放系统资源
return $return_content;
}
$url = "http://write.blog.csdn.net/postedit";//接口地址
$json_body =json_encode(Array('1'=>1,'2'=>2));//需要发送的数据
http_post_data($url, $json_body);
//http的xml post请求
public function httpPostData($url,$Xmldata){
$header[] = "Content-type: text/xml"; //定义content-type为xml,注意是数组
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $Xmldata);
$response = curl_exec($ch);
if(curl_errno($ch)){
printcurl_error($ch);
}
curl_close($ch);
return $response;
}