<?php
$spider = new SpiderExample();
///question/267782048
$result = $spider->getData('https://www.zhihu.com/question/267782048/answer/330283932', [], $method = 'post');
var_dump($result);

/**
 * Class SpiderExample
 */
class SpiderExample
{

    private $cookie;

    /**
     * SpiderExample constructor.
     * @param string $cookieFile
     */
    public function __construct($cookieFile = './tmp')
    {
        header("Content-type: text/html; charset=utf-8");
        set_time_limit(0);
        //设置cookie文件
        $this->cookie = tempnam('./tmp', 'cookieSpiderExample' . date('Y-m-d'));
    }

    /**
     * 需要登录的先登录
     * @param $url
     * @param $postData
     * @param string $method
     * @return array|bool
     */
    public function login($url, $postData, $method = 'POST')
    {
        return $this->query(['url' => $url, 'data' => $postData], true, $method);
    }

    /**
     * 获取数据
     * @param $url
     * @param $postData
     * @param string $method
     * @return array|bool
     */

    public function getData($url, $postData, $method = 'POST')
    {
        return $this->query(['url' => $url, 'data' => $postData], false, $method);
    }

    /**
     * @param array postData
     * [
     *      'url'=>'',//链接
     *      'data'=>''//数据
     * ]
     * @param bool type 是否是登录请求
     * @param string method POST请求/get
     * @return array|bool
     */
    private function query($postData, $type = false, $method = 'POST')
    {
        $curl = curl_init();
        if ($type)
            $cookie_type = CURLOPT_COOKIEJAR;
        else
            $cookie_type = CURLOPT_COOKIEFILE;

        curl_setopt_array($curl, array(
            CURLOPT_URL => $postData['url'],
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            $cookie_type => $this->cookie,
            CURLOPT_TIMEOUT => 120,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => $method,
            //是否验证https
            CURLOPT_SSL_VERIFYHOST => 0,
            CURLOPT_SSL_VERIFYPEER => 0,
            //是否跟随跳转
            CURLOPT_FOLLOWLOCATION=>true,
            CURLOPT_POSTFIELDS => http_build_query($postData['data']),
            CURLOPT_HTTPHEADER => array(
                "Cache-Control: no-cache",
                "Content-Type: application/x-www-form-urlencoded",
            ),
        ));
        $response = curl_exec($curl);
        $err = curl_error($curl);
        curl_close($curl);
        if ($err) {
            return false;
        } else {
            return ['code' => 0, 'data' => $response];
        }
    }

}

标签: none

仅有一条评论

  1. hhh hhh

    hhh

添加新评论