php curl

作者: cuihaomaster | 来源:发表于2018-09-26 09:54 被阅读0次
        /**
         * curl get
         */
        public function httpGet( $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 );
            //curl_setopt($oCurl, CURLOPT_VERBOSE, 1);
            curl_setopt($oCurl, CURLOPT_HEADER, 0);
    
            $sContent = curl_exec($oCurl);
            // $aStatus = curl_getinfo($oCurl);
            curl_close($oCurl);
    
            return $sContent;
        }
    
        /**
         * 以post方式提交到对应的接口url
         */
        public function postCurl( $postdata, $url, $second = 30) {
            $ch = curl_init();
            if(stripos($url,"https://")!==FALSE){
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
                curl_setopt($ch, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
            }
            //设置超时
            curl_setopt( $ch, CURLOPT_TIMEOUT, $second );
            curl_setopt( $ch, CURLOPT_URL, $url );
            //设置header
            curl_setopt( $ch, CURLOPT_HEADER, FALSE );
            //要求结果为字符串且输出到屏幕上
            curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE );
            //post提交方式
            curl_setopt( $ch, CURLOPT_POST, TRUE );
            curl_setopt( $ch, CURLOPT_POSTFIELDS, $postdata );
            //运行curl
            $data = curl_exec( $ch );;
            //返回结果
            if ( $data ) {
                curl_close( $ch );
                return $data;
            } else {
                $error = curl_errno( $ch );
                curl_close( $ch );
                throw new \Exception( "curl errno: $error" );
            }
        }
    

    相关文章

      网友评论

          本文标题:php curl

          本文链接:https://www.haomeiwen.com/subject/jhcdoftx.html