首页 > 编程学习 > PHP:POST发送JSON字符串
2014五月21

PHP:POST发送JSON字符串

/**  
 * 发送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);  
?>

718 total views, 1 views today

本文地址:https://blog.wizzer.cn/archives/2975 , 转载请保留.

本文目前尚无任何评论.

发表评论