Teraflopst
3/8/2019 - 8:14 AM

PHP 利用 curl 发送 post get请求

PHP 利用 curl 发送 post get请求

<?php

// 简化版 get、post

// 利用PHP的curl模拟GET功能
function geturl($url)
{
    $ch = curl_init(); //初始化curl
    curl_setopt($ch, CURLOPT_URL, $url); //指定请求链接
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //返回原生输出
    // curl_setopt($ch, CURLOPT_PROXY, "127.0.0.1"); //代理服务器地址
    // curl_setopt($ch, CURLOPT_PROXYPORT, "7890"); //代理服务器端口
    $output = curl_exec($ch); //执行curl
    curl_close($ch); //关闭curl
    return $output; //返回接收的数据
}

// 利用PHP的curl模拟POST功能
function req_post($url, $data) {
    $ch = curl_init(); //初始化curl
    curl_setopt($ch, CURLOPT_URL, $url); //指定请求的URL
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //设置返回原生输出
    curl_setopt($ch, CURLOPT_POST, 1); //设置为POST方式
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //要POST的数据
    $res = curl_exec($ch); //执行curl
    curl_close($ch); //关闭curl
    return $res; //返回接收的信息
}
<?php

// 来源:http://www.runoob.com/php/php-ref-curl.html

// GET 发送请求 json
function geturl($url)
{
    $headerArray = array(
        "Content-type:application/json;",
        "Accept:application/json"
    );
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($url, CURLOPT_HTTPHEADER, $headerArray);
    $output = curl_exec($ch);
    curl_close($ch);
    // $output = json_decode($output,true);
    return $output;
}

// POST 发送json数据
function posturl_json($url, $data)
{
    $data = json_encode($data);
    $headerArray = array(
        "Content-type:application/json;charset=utf-8",
        "Accept:application/json"
    );
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArray);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    // $output = json_decode($output,true);
    return $output;
}

// POST 发送文本数据
function posturl_text($url, $data)
{
    $headerArray = array(
        "Content-Type:text/plain;charset=utf-8",
        "Content-Length:" . strlen($data)
    );
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArray);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    // $output = json_decode($output,true);
    return $output;
}
<?php

// doHttpPost :循环执行POST请求,返回失败、返回200 结束
// 来源:https://ai.qq.com/doc/auth.shtml

function doHttpPost($url, $params)
{
    $curl = curl_init();
    $response = false;
    do {
        // 初始化curl
        curl_setopt($curl, CURLOPT_URL, $url);

        // 2. 设置 HTTP HEADER (例:表单POST)
        $head = array(
            'Content-Type: application/x-www-form-urlencoded'
        );
        curl_setopt($curl, CURLOPT_HTTPHEADER, $head);

        // 3. 设置HTTP BODY (URL键值对)
        $body = http_build_query($params);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_NOBODY, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

        // 4. 执行
        $response = curl_exec($curl);
        
        // 5. 返回判断
        if ($response === false) {
            $response = false;
            break;
        }

        $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        if ($code != 200) {
            $response = false;
            break;
        }
    }

    while (0);
    curl_close($curl);
    return $response;
}

// 请求示例
// 设置请求数据
$params = array(
    'key1'       => '示例的值',
    'key2'       => '示例仅供参考',
);

// 执行API调用
$url = 'https://api.ai.qq.com/path/to/api';
$response = doHttpPost($url, $params);
echo $response;