PHP CURL方法,GETPOST请求。
2019独角兽企业重金招聘Python工程师标准>>>
//get获取
function ihttp_get($url) {return ihttp_request($url);
}//post获取
function ihttp_post($url, $data) {$headers = array('Content-Type' => 'application/x-www-form-urlencoded');return ihttp_request($url, $data, $headers);
}function ihttp_request($url, $post = '', $extra = array(), $timeout = 60) {$urlset = parse_url($url);if(empty($urlset['path'])) {$urlset['path'] = '/';}if(!empty($urlset['query'])) {$urlset['query'] = "?{$urlset['query']}";}if(empty($urlset['port'])) {$urlset['port'] = $urlset['scheme'] == 'https' ? '443' : '80';}if(function_exists('curl_init') && function_exists('curl_exec')) {$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $urlset['scheme']. '://' .$urlset['host'].($urlset['port'] == '80' ? '' : ':'.$urlset['port']).$urlset['path'].$urlset['query']);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_HEADER, 1);if($post) {curl_setopt($ch, CURLOPT_POST, 1);if (is_array($post)) {$post = http_build_query($post);}curl_setopt($ch, CURLOPT_POSTFIELDS, $post);}curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1');if (!empty($extra) && is_array($extra)) {$headers = array();foreach ($extra as $opt => $value) {if (strexists($opt, 'CURLOPT_')) {curl_setopt($ch, constant($opt), $value);} elseif (is_numeric($opt)) {curl_setopt($ch, $opt, $value);} else {$headers[] = "{$opt}: {$value}";}}if(!empty($headers)) {curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);}}$data = curl_exec($ch);$status = curl_getinfo($ch);$errno = curl_errno($ch);curl_close($ch);if($errno || empty($data)) {return false;} else {return ihttp_response_parse($data);}}$method = empty($post) ? 'GET' : 'POST';$fdata = "{$method} {$urlset['path']}{$urlset['query']} HTTP/1.1\r\n";$fdata .= "Host: {$urlset['host']}\r\n";if(function_exists('gzdecode')) {$fdata .= "Accept-Encoding: gzip, deflate\r\n";}$fdata .= "Connection: close\r\n";if (!empty($extra) && is_array($extra)) {foreach ($extra as $opt => $value) {if (!strexists($opt, 'CURLOPT_')) {$fdata .= "{$opt}: {$value}\r\n";}}}$body = '';if ($post) {if (is_array($post)) {$body = http_build_query($post);} else {$body = urlencode($post);}$fdata .= 'Content-Length: ' . strlen($body) . "\r\n\r\n{$body}";} else {$fdata .= "\r\n";}if($urlset['scheme'] == 'https') {$fp = fsockopen('ssl://' . $urlset['host'], $urlset['port']);} else {$fp = fsockopen($urlset['host'], $urlset['port']);}stream_set_blocking($fp, true);stream_set_timeout($fp, $timeout);if (!$fp) {return false;} else {fwrite($fp, $fdata);$content = '';while (!feof($fp))$content .= fgets($fp, 512);fclose($fp);return ihttp_response_parse($content, true);}
}function ihttp_response_parse($data, $chunked = false) {$rlt = array();$pos = strpos($data, "\r\n\r\n");$split1[0] = substr($data, 0, $pos);$split1[1] = substr($data, $pos + 4, strlen($data));$split2 = explode("\r\n", $split1[0], 2);preg_match('/^(\S+) (\S+) (\S+)$/', $split2[0], $matches);$rlt['code'] = $matches[2];$rlt['status'] = $matches[3];$rlt['responseline'] = $split2[0];$header = explode("\r\n", $split2[1]);$isgzip = false;$ischunk = false;foreach ($header as $v) {$row = explode(':', $v);$key = trim($row[0]);$value = trim($row[1]);if (is_array($rlt['headers'][$key])) {$rlt['headers'][$key][] = $value;} elseif (!empty($rlt['headers'][$key])) {$temp = $rlt['headers'][$key];unset($rlt['headers'][$key]);$rlt['headers'][$key][] = $temp;$rlt['headers'][$key][] = $value;} else {$rlt['headers'][$key] = $value;}if(!$isgzip && strtolower($key) == 'content-encoding' && strtolower($value) == 'gzip') {$isgzip = true;}if(!$ischunk && strtolower($key) == 'transfer-encoding' && strtolower($value) == 'chunked') {$ischunk = true;}}if($chunked && $ischunk) {$rlt['content'] = ihttp_response_parse_unchunk($split1[1]);} else {$rlt['content'] = $split1[1];}if($isgzip && function_exists('gzdecode')) {$rlt['content'] = gzdecode($rlt['content']);}$rlt['meta'] = $data;if($rlt['code'] == '100') {return ihttp_response_parse($rlt['content']);}return $rlt;
}function ihttp_response_parse_unchunk($str = null) {if(!is_string($str) or strlen($str) < 1) {return false;}$eol = "\r\n";$add = strlen($eol);$tmp = $str;$str = '';do {$tmp = ltrim($tmp);$pos = strpos($tmp, $eol);if($pos === false) {return false;}$len = hexdec(substr($tmp, 0, $pos));if(!is_numeric($len) or $len < 0) {return false;}$str .= substr($tmp, ($pos + $add), $len);$tmp = substr($tmp, ($len + $pos + $add));$check = trim($tmp);} while(!empty($check));unset($tmp);return $str;
}
转载于:https://my.oschina.net/selly1025/blog/647403
PHP CURL方法,GETPOST请求。相关推荐
- curl post json_curl 模拟 GETPOST 请求,以及 curl post 上传文件
curl 模拟 GETPOST 请求,以及 curl post 上传文件 一般情况下,我们调试数据接口,都会使用一个 postman 的工具,但是这个工具还是有点大了.事实上,我们在调试一些小功能的时 ...
- php循环输出多个网络地址图片,php中curl循环往请求多个URL和多线程去请求多个URL的方法...
php 中curl 循环去请求多个URL和多线程去请求多个URL的方法 第一种:循环请求$sr=array(url_1,url_2,url_3); foreach ($sr as $k=>$v) ...
- shell处理curl返回数据_linux shell中curl 发送post请求json格式问题的处理方法
今天在linux中使用curl发送一个post请求时,带有json的数据,在发送时发现json中的变量没有解析出来 如下 curl -i -X POST -H 'Content-type':'appl ...
- curl模拟post请求
另外可尝试 postman工具 或者用request 直接请求 CURL 发送POST请求curl -header "Content-Type: application/json" ...
- php请求aspx,PHP用curl函数POST请求到ASP页面提示无效请求
如题,一提交即返回以下信息: 错误 您所请求的网址(URL)无法获取 ----------------------------------------------------------------- ...
- linux抓post命令,Linux 使用curl发起post请求的4个常用方式
引言 cURL是一种命令行实用程序,用于使用一种受支持的协议,从远程服务器传输数据,或将数据传输到远程服务器.默认情况下,已安装在macOS和大多数Linux发行版上. 开发人员可以使用cURL来测试 ...
- php curl发送post请求失败,php 利用curl发送post请求
利用curl发送post请求完成数据,接口的访问,这里面的参数可以修正一下,就可以写成可以支持文件上传的一个post请求,但是我这里不做仔细的写了.等以后有时间了在考虑怎么分解这个方法: /** * ...
- http 使用curl发起https请求
今天一个同事反映,使用curl发起https请求的时候报错:"SSL certificate problem, verify that the CA cert is OK. Details: ...
- php asp 发起post请求,PHP用curl函数POST请求到ASP页面提示无效请求
如题,一提交即返回以下信息: 错误 您所请求的网址(URL)无法获取 --------------------------– 当尝试进行以下请求时: POST /card/pay_card.aspx ...
- 用curl发起https请求
使用curl发起https请求 使用curl如果想发起的https请求正常的话有2种做法: 方法一.设定为不验证证书和host. 在执行curl_exec()之前.设置option $ch = cur ...
最新文章
- linux的veth导致网络不通,linux的veth对网桥通信实验
- TF:利用是Softmax回归+GD算法实现MNIST手写数字图片识别(10000张图片测试得到的准确率为92%)
- 02- Image Terminology
- C++服务器设计(七):聊天系统服务端实现
- MySQL操作之条件,排序,分页,聚合函数,分组,连接,子查询,自连接查询总结...
- matlab处理hdf5数据画图,读取行的最佳HDF5数据集块形状
- 每天00:00,MySQL定时弹出一个taskeng.exe
- 在下层捕获上层抛出的异常
- Java面试应该准备的知识点系列一
- 爬虫之异步爬虫asyncio
- 袁亚湘院士上《开讲啦》变数学魔术啦!
- TextView 在xml 中设置图片大小
- linux 提升管理员权限命令,​cmd怎么用命令直接提升到管理员权限|cmd原有权限提升方法...
- image caption
- yy自动语音接待机器人_YY自动欢迎老板,全自动欢迎,来人自动欢迎广播(文字欢迎)...
- MMC 事件查看器无法打开
- 2百度元老结论:web2.0创业时代将终结
- 西普实验吧CTF-Hashkill
- 2018腾讯秋招笔试题
- 如何在 Visual Paradigm 上执行静默安装