• curl 异步捉取数据类


    <?php
    
    class RequestLib
    {
    
        /**
         * GET 请求
         * @param string $url
         */
        public static function http_get($url)
        {
            $oCurl = curl_init();
            if (stripos($url, "https://") !== FALSE) {
                curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
                curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
                curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
            }
            curl_setopt($oCurl, CURLOPT_URL, $url);
            curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
            $sContent = curl_exec($oCurl);
            $aStatus = curl_getinfo($oCurl);
            curl_close($oCurl);
    
            if (intval($aStatus["http_code"]) == 200) {
                return $sContent;
            } else {
                return false;
            }
        }
    
        /**
         * POST 请求
         * @param string $url
         * @param array $param
         * @param boolean $post_file 是否文件上传
         * @return string content
         */
        public static function http_post($url, $param, $post_file = false, $isJson = false)
        {
            if (is_array($param) && count($param)) {
                $param = json_encode($param);
            }
            $oCurl = curl_init();
            if (stripos($url, "https://") !== FALSE) {
                curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
                curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
                curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
            }
            if (is_string($param) || $post_file) {
                $strPOST = $param;
            } else {
                $aPOST = array();
                foreach ($param as $key => $val) {
                    $aPOST[] = $key . "=" . urlencode($val);
                }
                $strPOST = join("&", $aPOST);
            }
            if (isset($_SESSION['CURLOPT_HTTPHEADER'])) {
                curl_setopt($oCurl, CURLOPT_HTTPHEADER, $_SESSION['CURLOPT_HTTPHEADER']);
            }
            if ($isJson) {
                curl_setopt($oCurl, CURLOPT_HTTPHEADER, array(
                        'Content-Type: application/json',
                        'Content-Length: ' . strlen($strPOST))
                );
            }
            curl_setopt($oCurl, CURLOPT_URL, $url);
            curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($oCurl, CURLOPT_POST, true);
            curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
            $sContent = curl_exec($oCurl);
            $aStatus = curl_getinfo($oCurl);
            curl_close($oCurl);
    
            if ($sContent) {
                return $sContent;
            } else {
                return false;
            }
        }
    
        /**
         * POST 请求
         * @param string $url
         * @param array $param
         * @param boolean $post_file 是否文件上传
         * @return string content
         */
        public static function http_post_async($url, $param, $post_file = false)
        {
            if (is_array($param) && count($param)) {
                $param = json_encode($param);
            }
            $oCurl = curl_init();
            if (stripos($url, "https://") !== FALSE) {
                curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
                curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
                curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
            }
            if (is_string($param) || $post_file) {
                $strPOST = $param;
            } else {
                $aPOST = array();
                foreach ($param as $key => $val) {
                    $aPOST[] = $key . "=" . urlencode($val);
                }
                $strPOST = join("&", $aPOST);
            }
            if (isset($_SESSION['CURLOPT_HTTPHEADER'])) {
                curl_setopt($oCurl, CURLOPT_HTTPHEADER, $_SESSION['CURLOPT_HTTPHEADER']);
            }
            curl_setopt($oCurl, CURLOPT_URL, $url);
            curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($oCurl, CURLOPT_NOSIGNAL, 1);//注意,毫秒超时一定要设置这个
            curl_setopt($oCurl, CURLOPT_TIMEOUT_MS, 50);//超时毫秒
            curl_setopt($oCurl, CURLOPT_POST, true);
            curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
            curl_exec($oCurl);
            curl_close($oCurl);
    
        }
    
        /**
         * 不支持中文转义的json结构
         * @param array $arr
         */
        static function json_encode($arr)
        {
            $parts = array();
            $is_list = false;
            //Find out if the given array is a numerical array
            $keys = array_keys($arr);
            $max_length = count($arr) - 1;
            if (($keys [0] === 0) && ($keys [$max_length] === $max_length)) { //See if the first key is 0 and last key is length - 1
                $is_list = true;
                for ($i = 0; $i < count($keys); $i++) { //See if each key correspondes to its position
                    if ($i != $keys [$i]) { //A key fails at position check.
                        $is_list = false; //It is an associative array.
                        break;
                    }
                }
            }
            foreach ($arr as $key => $value) {
                if (is_array($value)) { //Custom handling for arrays
                    if ($is_list)
                        $parts [] = self::json_encode($value); /* :RECURSION: */
                    else
                        $parts [] = '"' . $key . '":' . self::json_encode($value); /* :RECURSION: */
                } else {
                    $str = '';
                    if (!$is_list)
                        $str = '"' . $key . '":';
                    //Custom handling for multiple data types
                    if (!is_string($value) && is_numeric($value) && $value < 2000000000)
                        $str .= $value; //Numbers
                    elseif ($value === false)
                        $str .= 'false'; //The booleans
                    elseif ($value === true)
                        $str .= 'true';
                    else
                        $str .= '"' . addslashes($value) . '"'; //All other things
                    // :TODO: Is there any more datatype we should be in the lookout for? (Object?)
                    $parts [] = $str;
                }
            }
            $json = implode(',', $parts);
            if ($is_list)
                return '[' . $json . ']'; //Return numerical JSON
            return '{' . $json . '}'; //Return associative JSON
        }
    
    /**
     * curl 抓取图片
     * @param $url
     * @return mixed
     */
    public static function downLoadImage($url)
    {
        $header = array('Expect:');
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        $img = curl_exec($ch);
        curl_close($ch);
    
        //$return_code = curl_getinfo ( $ch, CURLINFO_HTTP_CODE );
        return $img;
    }
    }
  • 相关阅读:
    深度解析VC中的消息传递机制(上)
    DLL的远程注入技术
    一些游戏编程的书[转]
    [转]小小C的C++之歌
    Windows Server 2008无法使用arp命令添加静态MAC绑定
    如何调用未公开的API函数[转]
    IOCP中的socket错误和资源释放处理方法
    TinyXML应用例子
    微软C/C++ 编译器选项参考
    [摘录]这几本游戏编程书籍你看过吗?
  • 原文地址:https://www.cnblogs.com/-mrl/p/7147217.html
Copyright © 2020-2023  润新知