前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >远程请求

远程请求

作者头像
joshua317
发布2018-04-16 10:38:58
1K0
发布2018-04-16 10:38:58
举报
文章被收录于专栏:技术博文技术博文
代码语言:javascript
复制
function simpleRequest( $url , $post_data = array() ,$option=array())
{/*{{{*/
        //使用http_build_query拼接post
        if ( '' == $url )
        {
                return false;
        }
        $url_ary = parse_url( $url );
        if ( !isset( $url_ary['host'] ) )
        {
                return false;
        }
        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true);
    //curl_setopt($ch, CURLOPT_ENCODING ,'gzip'); //加入gzip解析

        curl_setopt( $ch, CURLOPT_HEADER, ($option['CURLOPT_HEADER']===true) );
        if($option['referer']!='')
        {
                curl_setopt( $ch, CURLOPT_REFERER, $option['referer']);
        }
        if(!empty($post_data))
        {
                curl_setopt( $ch, CURLOPT_POST, true);
                curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_data);
        }
        curl_setopt( $ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 6.00; Windows NT 5.0)' );

        $http_header = array();
        $http_header[] = 'Connection: close';
        $http_header[] = 'Pragma: no-cache';
        $http_header[] = 'Cache-Control: no-cache';
        $http_header[] = 'Accept: */*';
        if(isset($option['header']))
        {
                foreach($option['header'] as $header)
                {
                        $http_header[] = $header;
                }
        }
        curl_setopt( $ch, CURLOPT_HTTPHEADER, $http_header );

        curl_setopt( $ch, CURLOPT_URL, $url );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
        if ( !isset($option['timeout']))
        {
                $option['timeout'] = 10;
        }

        curl_setopt( $ch, CURLOPT_TIMEOUT, $option['timeout'] );
        $result = curl_exec( $ch );
        curl_close( $ch );
        return $result;
}/*}}}*/
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014-09-09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档