PHP remote request CURL case (crawler, save login status)

GET case

/** 
 * curl_get 
 * @param $url 
 * @param null $param 
 * @param null $options 
 * @return array 
 */function curl_get($url,$param = null,$options = null){ 
    if(empty($ options)){ 
        $options = array( 
            'timeout' => 30,// request 
            timeout'header' => array(), 
            'cookie' =>'',// cookie string, the browser can just copy it directly 
            ' cookie_file' =>'',// File path, and'ssl' with read and write permissions 
            => 0,// Whether to check https protocol'referer 
            ' => null 
        ); 
    }else{ 
        empty($options['timeout ']) && $options['timeout'] = 30;
        empty($options['ssl']) && $options['ssl']	= 0;
    }
    $result = array(
        'code'      => 0,
        'msg'       => 'success',
        'body'      => ''
    );
    if(is_array($param)){
        $param = http_build_query($param);
    }
    $url = strstr($url,'?')?trim($url,'&').'&'.$param:$url.'?'.$param;
    $ch = curl_init();

    curl_setopt($ch,CURLOPT_URL, $url);// 设置url
    !empty($options['header']) && curl_setopt($ch, CURLOPT_HTTPHEADER, $options['header']); // 设置请求头
    if(!empty($options['cookie_file']) && file_exists($options['cookie_file'])){
        curl_setopt($ch, CURLOPT_COOKIEFILE, $options['cookie_file']);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $options['cookie_file']);
    }else if(!empty($options['cookie'])){ 
        $result['code'] = 1; // error
        curl_setopt($ch, CURLOPT_COOKIE, $options['cookie']); 
    } 
    curl_setopt($ch, CURLOPT_ENCODING,'gzip'); //curl decompresses gzip page content 
    curl_setopt($ch, CURLOPT_HEADER, 0);// do not get Request header 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// output transfer, do not output page 
    ! $options['ssl'] && curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $options['ssl']); // prohibit server-side Verify ssl 
    !empty($options['referer']) && curl_setopt($ch, CURLOPT_REFERER, $options['referer']);//Disguise the request source and bypass the anti-theft 
    curl_setopt($ch, CURLOPT_TIMEOUT, $options[' timeout']); 
    //Execute and get the content 
    $output = curl_exec($ch); 
    //Operate the obtained content 
    if($output === FALSE ){ 
        $result['msg'] = "CURL Error:".curl_error($ch);
    }
    $result['body'] = $output; 
    //Release the curl handle 
    curl_close($ch); 
    return $result;}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758

POST case

/**
 * curl_post
 * @param $url              请求地址
 * @param null $param       get参数
 * @param array $options    配置参数
 * @return array
 */function curl_post($url,$param = null,$options = array()){
    if(empty($options)){
        $options = array(
            'timeout' 		=> 30,
            'header' 		=> array(),
            'cookie' 		=> '',
            'cookie_file'   => '',
            'ssl' 			=> 0,
            'referer' 		=> null
        );
    }else{
        empty($options['timeout']) && $options['timeout'] = 30;
        empty($options['ssl']) && $options['ssl']	= 0;
    }

    $result = array(
        'code'      => 0,
        'msg'       => 'success',
        'body'      => ''
    );
    if(is_array($param)){
        $param = http_build_query($param);
    }
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);// 设置url
    !empty($options['header']) && curl_setopt($ch, CURLOPT_HTTPHEADER, $options['header']); // 设置请求头
    if(!empty($options['cookie_file']) && file_exists($options['cookie_file'])){
        curl_setopt($ch, CURLOPT_COOKIEFILE, $options['cookie_file']);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $options['cookie_file']);
    }else if(!empty($options['cookie'])){
        curl_setopt($ch, CURLOPT_COOKIE, $options['cookie']); 
    }
    //Operate the acquired content


    curl_setopt($ch, CURLOPT_ENCODING,'gzip'); //curl decompress gzip page content 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $param); 
    curl_setopt($ch, CURLOPT_HEADER, 0); // Do not get the request header 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// Output transfer, do not output page 
    ! $options['ssl'] && curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $options['ssl']); / / Prohibit server-side verification ssl 
    !empty($options['referer']) && curl_setopt($ch, CURLOPT_REFERER, $options['referer']);//Disguise the source of the request and bypass the anti-theft 
    curl_setopt($ch, CURLOPT_TIMEOUT, $options['timeout']); 
    //Execute and get the content 
    $output = curl_exec($ch); 
    if($output === FALSE ){ 
        $result['code'] = 1; // Error
        $result['msg'] = "CURL Error:".curl_error($ch);
    }
    $result['body'] = $output;
    //释放curl句柄
    curl_close($ch);
    return $result;}

Guess you like

Origin blog.csdn.net/ld17822307870/article/details/112857768