zoukankan      html  css  js  c++  java
  • curl抓取数据


    微信公众号抓取数据,PHP中进行post提交

    
    /**
         *
         * curl 支持post
         * @param string $base_url 基础链接
         * @param array $query_data 需要请求的数据
         * @param string $method 方法 get/post
         * @param boolean $ssl 关闭ssl验证
         * @param integer $exe_timeout 执行超时时间
         * @param integer $conn_timeout 连接超时时间
         * @param integer $dns_timeout dns超时时间
         */
     function tx_curl($base_url, $query_data, $method = 'get', $ssl = true, $exe_timeout = 10, $conn_timeout = 10, $dns_timeout = 3600)
        {
            $ch = curl_init();
    
            if ( $method == 'get' ) {
                //method get
                if ( ( !empty($query_data) )
                    && ( is_array($query_data) )
                ){
                    $connect_symbol = (strpos($base_url, '?')) ? '&' : '?';
                    foreach($query_data as $key => $val) {
                        if ( is_array($val) ) {
                            $val = serialize($val);
                        }
                        $base_url .= $connect_symbol . $key . '=' . rawurlencode($val);
                        $connect_symbol = '&';
                    }
                }
            } else {
                if ( ( !empty($query_data) )
                    && ( is_array($query_data) )
                ){
                    foreach($query_data as $key => $val) {
                        if ( is_array($val) ) {
                            $query_data[$key] = serialize($val);
                        }
                    }
                }
                //method post
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $query_data);
            }
            curl_setopt($ch, CURLOPT_URL, $base_url);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $conn_timeout);
            curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, $dns_timeout);
            curl_setopt($ch, CURLOPT_TIMEOUT, $exe_timeout);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            // 关闭ssl验证
            if($ssl){
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            }
    
            $output = curl_exec($ch);
    
            if ( $output === FALSE )
                $output = '';
    
            curl_close($ch);
            return $output;
        }
    
    
  • 相关阅读:
    关于IE缓存的解决方案(HTML,JSP,ASP,PHP,C#)(转)
    ASP.NET 2.0 中的客户端脚本
    Post和Get的区别(兼谈页面间传值的方式)(转)
    委托的小例子
    JS的正则表达式
    HTTP消息头
    asp.net一个onclick的全过程(简单写一下)
    location.reload() 和 location.replace()的区别和应用
    使用 .NET Framework 2.0 在您的应用程序中支持证书(转)
    页面动态注册脚本(小技巧)
  • 原文地址:https://www.cnblogs.com/datiangou/p/10202666.html
Copyright © 2011-2022 走看看