get,post请求他人接口数据运用实例
http.php
<?phpnamespace appluckycommon;
/**
* Class Http
*/
class Http
{
private static $info = [];
/**
* @param $url
* @param array $headers
* @param array $options
* @return Requests_Response
*/
public static function get($url, array $headers = [], array $options = [])
{
$options = self::setOptions($options);
return self::call(__FUNCTION__, [$url, $headers, $options]);
}
/**
* @param $url
* @param array $headers
* @param array $options
* @return Requests_Response
*/
public static function head($url, array $headers = [], array $options = [])
{
$options = self::setOptions($options);
return self::call(__FUNCTION__, [$url, $headers, $options]);
}
/**
* @param $url
* @param array $headers
* @param array $options
* @return Requests_Response
*/
public static function delete($url, array $headers = [], array $options = [])
{
$options = self::setOptions($options);
return self::call(__FUNCTION__, [$url, $headers, $options]);
}
/**
* @param $url
* @param array $headers
* @param array $options
* @return Requests_Response
*/
public static function trace($url, array $headers = [], array $options = [])
{
$options = self::setOptions($options);
return self::call(__FUNCTION__, [$url, $headers, $options]);
}
/**
* @param $url
* @param array $headers
* @param array $data
* @param array $options
* @return Requests_Response
*/
public static function post($url, array $headers = [], $data = [], array $options = [])
{
$options = self::setOptions($options);
return self::call(__FUNCTION__, [$url, $headers, $data, $options]);
}
/**
* @param $url
* @param array $headers
* @param array $data
* @param array $options
* @return Requests_Response
*/
public static function put($url, array $headers = [], array $data = [], array $options = [])
{
$options = self::setOptions($options);
return self::call(__FUNCTION__, [$url, $headers, $data, $options]);
}
/**
* @param $url
* @param array $headers
* @param array $data
* @param array $options
* @return Requests_Response
*/
public static function options($url, array $headers = [], array $data = [], array $options = [])
{
$options = self::setOptions($options);
return self::call(__FUNCTION__, [$url, $headers, $data, $options]);
}
/**
* @param $url
* @param $headers
* @param array $data
* @param array $options
* @return Requests_Response
*/
public static function patch($url, array $headers, array $data = [], array $options = [])
{
$options = self::setOptions($options);
return self::call(__FUNCTION__, [$url, $headers, $data, $options]);
}
/**
* @param string $url
* @param array $headers
* @param array $data
* @param string $type
* @param array $options
* @return Requests_Response
*/
public static function request($url, array $headers = [], array $data = [], $type = Requests::GET, array $options = [])
{
$options = self::setOptions($options);
return self::call(__FUNCTION__, [$url, $headers, $data, $type, $options]);
}
/**
* @param array $requests
* @param array $options
* @return Requests_Response
*/
public static function request_multiple(array $requests, array $options)
{
return self::call(__FUNCTION__, [$requests, $options]);
}
private static function getHooks()
{
$hooks = new Requests_Hooks();
$hooks->register("curl.after_request", function ($headers, $info) {
self::$info = !is_array($info) ? [$info] : $info;
});
return $hooks;
}
private static function setOptions($options)
{
if (!isset($options["hooks"])) {
$options["hooks"] = self::getHooks();
}
$options["idn"] = false;
return $options;
}
private static function call($name, $args)
{
try {
$type = $name;
if ("request" === $name) {
$type = isset($args[3]) ? $args[3] : $name;
}
$type = strtolower($type);
//设置url
if ($type !== "request_multiple") {
$args[0] = new RequestsIRI($args[0]);
}
$response = call_user_func_array(["Requests", $name], $args);
if ($response instanceof Requests_Response && is_object($response->url)) {
$response->url = $response->url->uri;
}
return $response;
} catch (Exception $e) {
Log::error($e->getMessage());
$response = new Requests_Response();
$response->body = "";
return $response;
}
}
}
//业务代码
// www.cwl.gov.cn 请求头
private $cwlHeader = [
"X-Requested-With"=> "XMLHttpRequest",
"Referer" => "http://www.cwl.gov.cn/kjxx/"
];
protected function doGet($url, $urlParams = [], $headers = [], $options = [])
{
$response = appluckycommonHttp::request($url, $headers, $urlParams, "GET", $options);
return $response->body;
}
/**
* @param $num
* @return array
* @description 按期号爬(福彩3D)
*/
public function fcsdCrawlerByNum($num){
$url = "http://www.cwl.gov.cn/cwl_admin/kjxx/findKjxx/forIssue?name=3d&code=".$num;
$data = $this->doGet($url,[],$this->cwlHeader);
$data = json_decode($data,true);
if($data["state"] != _MSG_SUCCESS){
Redis::getInstance()->redisHSetTrue(_REDIS_CRAWLER_STATUS,"fcsdCrawlerByNum");
Log::warning("现在已经爬不到 福彩3D 开奖结果了(中国福利彩票发行管理中心官方网站 www.cwl.gov.cn) fcsdCrawlerByNum()", []);
return ["code"=>_MSG_SYSTEM_ERROR, "msg"=>$data["message"]];
}
Redis::getInstance()->redisHSetFalse(_REDIS_CRAWLER_STATUS,"fcsdCrawlerByNum");
return $this->fcsdDataFormat($data["result"]);
}
另外一种的业务逻辑PHP代码块
/** * @param $lotteryId
* @return array
* @description 地方彩往期爬取
*/
public function dfCrawlerM500List($lotteryId)
{
$id500 = $this->fu51ToWb[$lotteryId];
$url = "https://m.500.com/info/kaijiang/moreexpect/" . $id500 . "/?from=";
$result = $this->doGet($url, [], $this->m500Header);
$encode = mb_detect_encoding($result, array("ASCII", "UTF-8", "GB2312", "GBK", "BIG5"));
$result = mb_convert_encoding($result, "UTF-8", $encode);
$pattern = "/<div.*?>.*?([0-9]{5,10}).*?<em>([0-9-]{10}).*?</em>s*</div>s*<div.*?>s*<ul.*?>(.*?)</ul>s*</div>/ism"; //
if (preg_match_all($pattern, $result, $matches)) {
unset($matches[0]);
$data = [];
foreach ($matches[1] as $key => $value) {
if (preg_match_all("/<li.*?>(.*?)</li>/ism", $matches[3][$key], $res)) {
unset($res[0]);
$res = $res[1];
} else {
continue;
}
$data[] = [
"lottery_id" => $lotteryId,
"lottery_name" => $this->dfName[$lotteryId],
"lottery_no" => $matches[1][$key],
"lottery_date" => $matches[2][$key],
"lottery_res" => $res,
];
}
unset($matches);
unset($result);
} else {
Redis::getInstance()->redisHSetTrue(_REDIS_CRAWLER_STATUS, "dfCrawlerM500List");
Log::warning("现在已经爬不到 地方彩 历史开奖结果了(500彩票网 m.500.com) dfCrawlerM500List()", []);
return ["code" => _MSG_SYSTEM_ERROR, "msg" => "查询失败", "data" => []];
}
Redis::getInstance()->redisHSetFalse(_REDIS_CRAWLER_STATUS, "dfCrawlerM500List");
return ["code" => _MSG_SYSTEM_SUCCESS, "msg" => "查询成功", "data" => $data];
}
$pattern = "/<div.*?>.*?([0-9]{5,10}).*?<em>([0-9-]{10}).*?</em>s*</div>s*<div.*?>s*<ul.*?>(.*?)</ul>s*</div>/ism"; //对于的html代码块
/*
<div class="kaij-tit ">2020009期<em>2020-01-20 星期一</em> </div>
<div class="kaij-jg ">
<ul>
<li class="red-ball" >3</li>
<li class="red-ball" >2</li>
<li class="red-ball" >2</li>
<li class="red-ball" >5</li>
<li class="red-ball" >8</li>
</ul>
</div>
*/
POST方法处理
/* * 球队球员转入
*/
public function getPlayerShiftTo($teamId, $page_no, $page_count)
{
try {
$uri = "sports/team/player_shiftTo";
$data = Http::post($this->url . $uri, [], ["team_id" => $teamId, "page_no" => $page_no, "page_count" => $page_count]);
$data = json_decode($data->body, true);
if (empty($data)) {
throw new Exception("服务接口出现错误 -1");
}
return $data;
} catch (Exception $e) {
Log::error($e->getMessage());
return ["code" => -1, "msg" => $e->getMessage()];
}
}
Buy me a cup of coffee :)
以上是 get,post请求他人接口数据运用实例 的全部内容, 来源链接: utcz.com/z/514089.html