PHP调用示例代码

 $val) {
			$ret .= $key . "=" . $val . "&";
		}
		$ret .= "devSecret=" . self::$APP_SECRET;
		return $ret;
	}

	private static function buildHttpQuery($url, $parameter = array()) {
		ksort($parameter);
		$sign = sha1(self::obj2strAndAppendSecret($parameter));

		$ret = "";
		foreach ($parameter as $key => $val) {
			$ret .= $key . "=" . $val . "&";
		}
		$ret .= "signature=" . $sign;
		$url = $url . "?" . utf8_encode($ret);
		return $url;
	}
	
	// 生成毫秒时间戳
	private static function getMillisecond() { 
		list($s1, $s2) = explode(' ', microtime()); 
		return (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
	}
	
	// 发出请求,返回结果
	private static function get($api, $data = array()) {
		$data["developerId"] = self::$APP_KEY;
		$data["timestamp"] = self::getMillisecond();
		
		$url = self::buildHttpQuery(self::$HOST . $api, $data);
		$url = str_replace(" ", "%20", $url);
		$response = file_get_contents($url);

		return json_decode($response, true);
	}

	/** 获取科目列表
	 * @param $stage 学段,小学1,初中2,高中3
	 */
	public static function getSubjectList($stage=2) {
		$result = self::get("subject/list", array(
				"stage" => $stage,
			));

		return $result;
	}

	/** 获取教材版本
	 * @param $sid 科目id
	 */
	public static function getVersionList($sid=2) {
		$result = self::get("version/list", array(
				"sid" => $sid,
			));

		return $result;
	}

	/** 获取教材版本
	 * @param $vid 版本id
	 */
	public static function getTextbookList($vid=2) {
		$result = self::get("textbook/list", array(
				"vid" => $vid,
			));

		return $result;
	}

}
$apiCall = new ApiCall();
$apiCall::getSubjectList();

?>

你可能感兴趣的:(PHP调用示例代码)