欢迎光临
我们一直在努力

PHP中使用CURL实现GET、POST、PUT、DELETE请求

做淘宝客,有些接口的权限没有,找到一个免费提供相关api的接口,请求方式POST,找了一个函数记录一下

/**
 * @param $url
 * @param $data
 * @param string $method
 * @param string $type
 * @return bool|string
 */
function curlData($url,$data,$method = 'GET',$type='json')
{
    //初始化
    $ch = curl_init();
    $headers = [
        'form-data' => ['Content-Type: multipart/form-data'],
        'json'      => ['Content-Type: application/json'],
    ];
    if($method == 'GET'){
        if($data){
            $querystring = http_build_query($data);
            $url = $url.'?'.$querystring;
        }
    }
    // 请求头,可以传数组
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_HTTPHEADER,$headers[$type]);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);         // 执行后不直接打印出来
    if($method == 'POST'){
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'POST');     // 请求方式
        curl_setopt($ch, CURLOPT_POST, true);               // post提交
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);              // post的变量
    }
    if($method == 'PUT'){
        curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "PUT");
        curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
    }
    if($method == 'DELETE'){
        curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
        curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
    }
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 不从证书中检查SSL加密算法是否存在
    $output = curl_exec($ch); //执行并获取HTML文档内容
    curl_close($ch); //释放curl句柄
    return $output;
}
分享到: 更多 (0)

点击此处获取更多金融后续培训参考答案

相关推荐


  • Notice: Undefined index: taxonomy in /data/wwwroot/houjianfang.com/wp-content/plugins/wpjam-basic/public/wpjam-thumbnail.php on line 84

    Notice: Undefined index: post_meta in /data/wwwroot/houjianfang.com/wp-content/plugins/wpjam-basic/public/wpjam-thumbnail.php on line 78

    Notice: Undefined index: taxonomy in /data/wwwroot/houjianfang.com/wp-content/plugins/wpjam-basic/public/wpjam-thumbnail.php on line 84

    Notice: Undefined index: post_meta in /data/wwwroot/houjianfang.com/wp-content/plugins/wpjam-basic/public/wpjam-thumbnail.php on line 78
    ionic项目中列表显示不同模板-侯建方的个人网站ionic项目中列表显示不同模板

  • Notice: Undefined index: taxonomy in /data/wwwroot/houjianfang.com/wp-content/plugins/wpjam-basic/public/wpjam-thumbnail.php on line 84

    Notice: Undefined index: post_meta in /data/wwwroot/houjianfang.com/wp-content/plugins/wpjam-basic/public/wpjam-thumbnail.php on line 78

    Notice: Undefined index: taxonomy in /data/wwwroot/houjianfang.com/wp-content/plugins/wpjam-basic/public/wpjam-thumbnail.php on line 84

    Notice: Undefined index: post_meta in /data/wwwroot/houjianfang.com/wp-content/plugins/wpjam-basic/public/wpjam-thumbnail.php on line 78
    php数据库操作类-侯建方的个人网站php数据库操作类

  • Notice: Undefined index: taxonomy in /data/wwwroot/houjianfang.com/wp-content/plugins/wpjam-basic/public/wpjam-thumbnail.php on line 84

    Notice: Undefined index: post_meta in /data/wwwroot/houjianfang.com/wp-content/plugins/wpjam-basic/public/wpjam-thumbnail.php on line 78

    Notice: Undefined index: taxonomy in /data/wwwroot/houjianfang.com/wp-content/plugins/wpjam-basic/public/wpjam-thumbnail.php on line 84

    Notice: Undefined index: post_meta in /data/wwwroot/houjianfang.com/wp-content/plugins/wpjam-basic/public/wpjam-thumbnail.php on line 78
    php的多态实现-侯建方的个人网站php的多态实现

  • Notice: Undefined index: taxonomy in /data/wwwroot/houjianfang.com/wp-content/plugins/wpjam-basic/public/wpjam-thumbnail.php on line 84

    Notice: Undefined index: post_meta in /data/wwwroot/houjianfang.com/wp-content/plugins/wpjam-basic/public/wpjam-thumbnail.php on line 78

    Notice: Undefined index: taxonomy in /data/wwwroot/houjianfang.com/wp-content/plugins/wpjam-basic/public/wpjam-thumbnail.php on line 84

    Notice: Undefined index: post_meta in /data/wwwroot/houjianfang.com/wp-content/plugins/wpjam-basic/public/wpjam-thumbnail.php on line 78
    JS一维数组、多维数组和对象的混合使用-侯建方的个人网站JS一维数组、多维数组和对象的混合使用

  • Notice: Undefined index: taxonomy in /data/wwwroot/houjianfang.com/wp-content/plugins/wpjam-basic/public/wpjam-thumbnail.php on line 84

    Notice: Undefined index: post_meta in /data/wwwroot/houjianfang.com/wp-content/plugins/wpjam-basic/public/wpjam-thumbnail.php on line 78

    Notice: Undefined index: taxonomy in /data/wwwroot/houjianfang.com/wp-content/plugins/wpjam-basic/public/wpjam-thumbnail.php on line 84

    Notice: Undefined index: post_meta in /data/wwwroot/houjianfang.com/wp-content/plugins/wpjam-basic/public/wpjam-thumbnail.php on line 78
    转|PHP判断用户访问的操作系统,以及iOS的设备-侯建方的个人网站转|PHP判断用户访问的操作系统,以及iOS的设备

  • Notice: Undefined index: taxonomy in /data/wwwroot/houjianfang.com/wp-content/plugins/wpjam-basic/public/wpjam-thumbnail.php on line 84

    Notice: Undefined index: post_meta in /data/wwwroot/houjianfang.com/wp-content/plugins/wpjam-basic/public/wpjam-thumbnail.php on line 78

    Notice: Undefined index: taxonomy in /data/wwwroot/houjianfang.com/wp-content/plugins/wpjam-basic/public/wpjam-thumbnail.php on line 84

    Notice: Undefined index: post_meta in /data/wwwroot/houjianfang.com/wp-content/plugins/wpjam-basic/public/wpjam-thumbnail.php on line 78
    wordpress英文主题截取中文不显示-侯建方的个人网站wordpress英文主题截取中文不显示
  • 被遗忘的HTML标签fieldset-侯建方的个人网站被遗忘的HTML标签fieldset