PHP(CURL)POST数据调用API简单示例

<?php
/**
 *@一个完整的POST调用API的过程 百度知道
 *@author: bo.xiao  
 */
	$url = 'http://zhidao.chanjet.com/restserver/zhidao';
	$data = array(
		'api_key'=>'3qQ2Edm62Vd4bAVCwNoxgn0l',
		'method'=>'baidu.zhidao.getQuestionList',
		'call_id'=>'1308713190',
		'cid'=>59533,
		'qstatus'=>1,
		'format'=>'json',
		'page_no'=>1,
		'page_size'=>25,
		'keywords'=>'财务',
		'bd_sig'=>'2bad1c47bb75e0363a689f4b09743afb'
	);

	$json_data = postData($url, $data);
	$array = json_decode($json_data,true);
	echo '<pre>';print_r($array);
	
	function postData($url, $data)
	{
		$ch = curl_init();
		$timeout = 300; 
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
		$handles = curl_exec($ch);
		curl_close($ch);
		return $handles;
	}

?>

你可能感兴趣的:(PHP,json,百度)