热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

get,post请求他人接口数据运用实例

http.php<?phpnamespaceappluckycommon;***ClassHttp*classHttp{privatestatic$info[];***@pa

http.php

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 = "/.*?([0-9]{5,10}).*?([0-9-]{10}).*?s*
s*s*(.*?)s*
/ism"; // if (preg_match_all($pattern, $result, $matches)) { unset($matches[0]); $data = []; foreach ($matches[1] as $key => $value) { if (preg_match_all("/(.*?)/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 = "/.*?([0-9]{5,10}).*?([0-9-]{10}).*?s*
s*s*(.*?)s*/ism"; //对于的html代码块 /*
2020009期2020-01-20 星期一
  • 3
  • 2
  • 2
  • 5
  • 8
*/

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 :)

 


推荐阅读
author-avatar
Tags | 热门标签
RankList | 热门文章
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有