qixidi

/**
     * 
     * 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;
    }

分类:

技术点:

相关文章:

  • 2022-01-09
  • 2022-02-04
  • 2021-11-29
  • 2022-01-14
  • 2021-12-22
  • 2021-11-26
  • 2021-11-20
  • 2021-11-09
猜你喜欢
  • 2022-01-11
  • 2022-01-03
  • 2021-12-19
  • 2021-12-25
  • 2021-05-30
  • 2021-11-17
相关资源
相似解决方案