PHP:POST发送JSON字符串

jerry PHP 2015年11月23日 收藏
/**  
 * 发送post请求  
 * @param string $url 请求地址  
 * @param array $post_data post数据  
 * @return string  
 */  
function send_post($url, $post_data) {
	$ch = curl_init($url);
	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
	curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
	curl_setopt($ch, CURLOPT_HTTPHEADER, array(
		'Content-Type: application/json',
		'Content-Length: ' . strlen($post_data))
	);  
	return curl_exec($ch);   
}   


<?php
	header("Content-type: text/html; charset=utf-8");
	include_once('../functions.php');
	$send_msg="test";//发送内容
	$data = array("openid" => "oXS2NuPCEB836NRMrsXXXXXX", "content" => $send_msg);
        $data_string = json_encode($data);
	echo send_post('http://XXX.cn/api/wx/push/custom/text?mykey=XXXXXX', $data_string);  
?>