php 卷曲请求类(GET,POST)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 卷曲请求类(GET,POST)相关的知识,希望对你有一定的参考价值。

<?php
/**
 * Created by PhpStorm.
 * User: kk
 * Date: 2017/4/23
 * Time: 下午2:22
 */

namespace App\Utils;


class HttpCurl
{

    /**
     * 通过 CURL post数据 调用接口
     * @param  [string]  $uri            [description]
     * @param  [array]  $postData       [description]
     * @param  [boolean]  $postJson     [description]
     * @param  integer $connecttimeout [description]
     * @param  integer $timeout        [description]
     * @return [json]                  [description]
     * @author hujiashan <[<email address>]>
     */
    public static function postCURL($uri, $postData, $postJson = false, $connecttimeout = 300, $timeout = 200)
    {
        if(empty($uri) || empty($postData)) return false;

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $uri);
        curl_setopt($ch, CURLOPT_POST, true);
        if($postJson){
            $postData = json_encode($postData,JSON_UNESCAPED_UNICODE);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                    'Content-Type: application/json; charset=utf-8',
                    'Content-Length: '.strlen($postData))
            );
        }else{
            $postData = http_build_query($postData);
        }
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $connecttimeout);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        $result = curl_exec($ch);
        curl_close($ch);

        return $result;
    }


    /**
     * 通过 CURL get数据 调用接口
     * @param  [string]  $uri            [description]
     * @param  integer $connecttimeout [description]
     * @param  integer $timeout        [description]
     * @return [bool|json]                  [description]
     */
    public static function getCURL($uri, $connecttimeout = 300, $timeout = 200)
    {
        if(empty($uri)) return false;

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $connecttimeout);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_URL, $uri);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

        $result = curl_exec($ch);
        curl_close($ch);

        return $result;
    }


}

以上是关于php 卷曲请求类(GET,POST)的主要内容,如果未能解决你的问题,请参考以下文章

处理大卷曲响应 - PHP

PHP post & get请求

php如何发起POST DELETE GET POST 请求

PHP 卷曲,POST JSON

HTTP请求类,便于POST/GET操作

PHP中使用cURL实现Get和Post请求的方法