PHP灌水機器人實現原理

實現原理:
通過curl工具模擬登錄,然後調用相關接口發送數據進行各種操作。

需要掌握知識點:

  • curl的POST/GET操作
  • curl發送header頭信息
  • curl接受保存來自服務端的cookie
  • curl發送cookie

代碼示範

  1. curl的GET操作

    
    private function projectCollections(): array
    {
        $ts = microtime(true) * 1000;
        $api = "http://www.****.com/json/projectCollections?status=1&username={$this->userName}&limit=1&projectID={$this->projectID}&ts=" . $ts;   //API地址
    
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $api);
        curl_setopt($curl, CURLOPT_HEADER, 0);//是否顯示頭信息
        curl_setopt($curl, CURLOPT_COOKIEJAR, $this->cookie); //設置Cookie信息保存在指定的文件中
        curl_setopt($curl, CURLOPT_COOKIEFILE, $this->cookie); //發送cookie信息
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_REFERER, "http://www.***.com/details/v5?id={$this->projectID}&isView=true");
        curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36");
        $json = curl_exec($curl);
        curl_close($curl);
    
        \Log::error(var_export($json, true));
        return json_decode($json, true);
        //{"meta":{"total":"0","start":"1","size":"0"},"data":[]}
    }
  2. curl的POST操作
//收藏
    private function addCollection()
    {
        $api = "http://www.****.com/sjc/api/project/collection/add";

        $postData = [
            'id' => (string)$this->projectID,
        ];
        $data = json_encode($postData);
        $length = strlen($data);

        $headers = [
            //'Origin:http://www.ilab-x.com',
            //'Host:www.ilab-x.com',
            "Content-type: application/json",
            'Content-Length: ' . $length,
        ];
        \Log::error(var_export($postData, true));
        \Log::error(var_export($data, true));

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $api);
        curl_setopt($curl, CURLOPT_HEADER, 0);//是否顯示頭信息
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_COOKIEJAR, $this->cookie); //設置Cookie信息保存在指定的文件中
        curl_setopt($curl, CURLOPT_COOKIEFILE, $this->cookie); //發送cookie信息
        curl_setopt($curl, CURLOPT_POST, 1);//post方式提交
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);//注意,這裏提交json格式
        curl_setopt($curl, CURLOPT_REFERER, "http://www.****.com/details/v5?id={$this->projectID}&isView=true");
        curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36");
        curl_setopt($curl, CURLOPT_ENCODING, 'deflate');

        $json = curl_exec($curl);
        curl_close($curl);
        \Log::error(var_export($headers, true));
        \Log::error(var_export($json, true));
        return json_decode($json, true);
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章