PHP-based brain teaser interface calling code example
Before calling this interface, you need to apply for APPKEY first. Apply for the brain teaser interface: https://www.juhe.cn/docs/api/id/186 // +-------------------------------------------------- -----------------------
// | JuhePHP [NO ZUO NO DIE]
// +-------------------------------------------------- -----------------------
// | Copyright (c) 2010-2015 http://juhe.cn All rights reserved.
// +-------------------------------------------------- -----------------------
// | Author: Juhedata
// +-------------------------------------------------- -----------------------
//----------------------------------
//Brain teaser call sample code - Aggregated data
// Online interface documentation: http://www.juhe.cn/docs/186
//----------------------------------
header('Content-type:text/html;charset=utf-8');
//Configure the appkey you applied for
$appkey = "**********************";
//************1.Brain teaser query************
$url = "http://xiaosilk.api.juhe.cn/njjzw/query";
$params = array(
"key" => $appkey,//APPKEY you applied for
"pagesize" => "",//The number returned per page, default 10, maximum 20
"pagenum" => "",//Page number, default 1
"tag" => "",//Type, default random
);
$paramstring = http_build_query($params);
$content = juhecurl($url,$paramstring);
$result = json_decode($content,true);
if($result){
If($result['error_code']=='0'){
print_r($result);
}else{
echo $result['error_code'].":".$result['reason'];
}
}else{
echo "Request failed";
}
//************************************************ **
/**
* Request the interface to return the content
* @param string $url [Requested URL address]
* @param string $params [requested parameters]
* @param int $ipost [Whether to use POST form]
* @return string
*/
function juhecurl($url,$params=false,$ispost=0){
$httpInfo = array();
$ch = curl_init();
curl_setopt( $ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt( $ch, CURLOPT_USERAGENT , 'JuheData' );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
If($ispost)
{
curl_setopt( $ch , CURLOPT_POST , true );
curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
curl_setopt( $ch , CURLOPT_URL , $url );
}
else
{
if($params){
curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
}else{
curl_setopt( $ch , CURLOPT_URL , $url);
}
}
$response = curl_exec( $ch );
if ($response === FALSE) {
//echo "cURL Error: " . curl_error($ch);
return false;
}
$httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
$httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
curl_close( $ch );
return $response;
}