分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

CURL 模拟post和get请求

发布时间:2023-09-06 02:34责任编辑:白小东关键词:暂无标签
<?php ????????class Curl ???{ ???????public static function get($url) ???????{ ???????????//创建一个新的CURL资源赋给变量$ch; ???????????$ch = curl_init(); ???????????//设置URL 及其他选项 ???????????curl_setopt($ch, CURLOPT_URL, $url); ???????????//设置获取的内容但不输出 ???????????curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); ???????????//设置输出的头信息 ???????????// curl_setopt($ch, CURLOPT_HEADER, 0); ???????????//执行 获取url内容并输出到浏览器 ???????????$output = curl_exec($ch); ???????????//释放资源 ???????????curl_close($ch); ???????????//返回获取的网页内容 ???????????return $output; ???????} ???????public static function post($url, $data) ???????{ ???????????//创建一个新的CURL资源赋给变量$ch ???????????$ch = curl_init(); ???????????if(class_exists(‘./CURLFile‘))//php5.5跟php5.6中的CURLOPT_SAFE_UPLOAD的默认值不同 ???????????{ ???????????????curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true); ???????????}else ???????????{ ???????????????if(defined(‘CURLOPT_SAFE_UPLOAD‘)) ???????????????{ ???????????????????curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false); ???????????????} ???????????} ???????????//设置要访问的url地址 ???????????curl_setopt($ch, CURLOPT_URL, $url); ???????????//设置获取的内容但不输出 ???????????curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); ???????????// 发送一个post的请求 ???????????curl_setopt($ch, CURLOPT_POST, 1); ???????????// post提交的数据包 ???????????curl_setopt($ch, CURLOPT_POSTFIELDS, $data); ???????????//执行操作 ???????????$output = curl_exec($ch); ???????????//关闭curl ???????????curl_close($ch); ???????????//返回数据 ???????????return $output; ???????} ???}

CURL 模拟post和get请求

原文地址:https://www.cnblogs.com/itsc/p/10468480.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved