[小技巧] 【使用Curl组件GET/POST数据】
来源:互联网 发布:unity3d 编辑器扩展 编辑:程序博客网 时间:2024/05/22 01:33
- //Curl Get数据
- function _curl_get($url) {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL,$url);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $data = curl_exec($ch);
- curl_close($ch);
- if ($data)
- return $data;
- else
- return false;
- }
- //Curl Post数据
- function _curl_post($url, $vars) {
- $ch = curl_init();
- curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_URL,$url);
- curl_setopt($ch, CURLOPT_POST, 1 );
- curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
- $data = curl_exec($ch);
- curl_close($ch);
- if ($data)
- return $data;
- else
- return false;
- }
通用函数
- /**
- * 使用GET方法发送HTTP请求
- *
- * @param string $url 需要请求的URL,完整URL,例如:http://www.example.com:8080/test.php?parm1=var1&parm2=var2
- * @param array/string $cookies 如果有COOKIE数据可以发送过去,可以是Cookie数组,也可以是Cookie字符串
- * @return mixed 成功返回GET回来的数据,失败返回false
- */
- function http_get($url, $cookies = array()) {
- /**
- * 使用cURL处理GET请求
- */
- if (function_exists('curl_init')){
- //组织COOKIE数据
- $header = array();
- if (!emptyempty($cookies)){
- if (is_array($cookies)){
- $encoded = '';
- while (list($k,$v) = each($cookies)) {
- $encoded .= ($encoded ? ";" : "");
- $encoded .= rawurlencode($k)."=".rawurlencode($v);
- }
- $header = array("Cookie :". $encoded);
- } elseif (is_string($cookies)){
- if (strtolower(substr($cookies, 0, 7)) == 'cookie:'){
- $header = array($cookies);
- } else {
- $header = array("Cookie: ". $cookies);
- }
- }
- }
- //处理请求
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL,$url);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $data = curl_exec($ch);
- curl_close($ch);
- if ($data)
- return $data;
- else
- return false;
- }
- /**
- * 使用fsockopen处理GET请求
- */
- else {
- //组织COOKIE数据
- $cookie = '';
- if (!emptyempty($cookies)){
- if (is_array($cookies)){
- $encoded = '';
- while (list($k,$v) = each($cookies)) {
- $encoded .= ($encoded ? ";" : "");
- $encoded .= rawurlencode($k)."=".rawurlencode($v);
- }
- $cookie = $encoded;
- } elseif (is_string($cookies)){
- if (strtolower(substr($cookies, 0, 7)) == 'cookie:'){
- $cookie = substr($cookies, 7);
- } else {
- $cookie = $cookies;
- }
- }
- }
- //处理请求
- $url = parse_url($url);
- if (strtolower($url['scheme']) != 'http' && $url['scheme'] != ''){
- return false;
- }
- if ( !($fp = fsockopen($url['host'], $url['port'] ? $url['port'] : 80, $errno, $errstr, 10))){
- return false;
- }
- fputs($fp, sprintf("GET %s%s%s HTTP/1.0/n", $url['path'], $url['query'] ? "?" : "", $url['query']));
- fputs($fp, "Host: $url[host]/n");
- fputs($fp, "User-Agent: HFHttp-Client/n");
- if ($cookie != ''){
- fputs($fp, "Cookie: $cookie/n/n");
- }
- fputs($fp, "Connection: close/n/n");
- fputs($fp, "$post /n");
- $ret = '';
- while (!feof($fp)) {
- $c = fgets($fp, 1024);
- if (preg_match("/^HTTP/", $c) || preg_match("/^P3P/", $c) || preg_match("/^([A-Za-z/-]+)/:/s*(.*)$/", $c)){
- continue;
- }
- if (trim($c) == ""){
- continue;
- }
- $ret .= $c;
- }
- fclose($fp);
- return $ret;
- }
- }
- /**
- * 使用POST方法发送HTTP请求
- *
- * @param string $url 需要请求的URL,完整URL,例如:http://www.example.com:8080/test.php?parm1=var1&parm2=var2
- * @param array $vars 需要POST提交的变量数组
- * @param array/string $cookies 如果有COOKIE数据可以发送过去,可以是Cookie数组,也可以是Cookie字符串
- * @return mixed 成功返回GET回来的数据,失败返回false
- */
- function http_post($url, $vars = array(), $cookies = array()) {
- /**
- * 使用cURL处理POST请求
- */
- if (function_exists('curl_init')){
- //组织COOKIE数据
- $header = array();
- if (!emptyempty($cookies)){
- if (is_array($cookies)){
- $encoded = '';
- while (list($k,$v) = each($cookies)) {
- $encoded .= ($encoded ? ";" : "");
- $encoded .= rawurlencode($k)."=".rawurlencode($v);
- }
- $header = array("Cookie :". $encoded);
- } elseif (is_string($cookies)){
- if (strtolower(substr($cookies, 0, 7)) == 'cookie:'){
- $header = array($cookies);
- } else {
- $header = array("Cookie: ". $cookies);
- }
- }
- }
- //执行POST请求
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL,$url);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_POST, 1 );
- curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $data = curl_exec($ch);
- curl_close($ch);
- if ($data)
- return $data;
- else
- return false;
- }
- /**
- * 使用fsockopen处理POST请求
- */
- else {
- //组织COOKIE数据
- $cookie = '';
- if (!emptyempty($cookies)){
- if (is_array($cookies)){
- $encoded = '';
- while (list($k,$v) = each($cookies)) {
- $encoded .= ($encoded ? ";" : "");
- $encoded .= rawurlencode($k)."=".rawurlencode($v);
- }
- $cookie = $encoded;
- } elseif (is_string($cookies)){
- if (strtolower(substr($cookies, 0, 7)) == 'cookie:'){
- $cookie = substr($cookies, 7);
- } else {
- $cookie = $cookies;
- }
- }
- }
- //组织POST数据
- $post = '';
- if (!emptyempty($vars)){
- if (is_array($vars)){
- $encoded = '';
- while (list($k,$v) = each($vars)) {
- $encoded .= ($encoded ? "&" : "");
- $encoded .= rawurlencode($k)."=".rawurlencode($v);
- }
- $post = $encoded;
- } else {
- $post = $vars;
- }
- }
- //处理请求
- $url = parse_url($url);
- if (strtolower($url['scheme']) != 'http' && $url['scheme'] != ''){
- return false;
- }
- if ( !($fp = fsockopen($url['host'], $url['port'] ? $url['port'] : 80, $errno, $errstr, 10))){
- return false;
- }
- fputs($fp, sprintf("POST %s%s%s HTTP/1.0/n", $url['path'], $url['query'] ? "?" : "", $url['query']));
- fputs($fp, "Host: $url[host]/n");
- fputs($fp, "User-Agent: HFHttp-Client/n");
- if ($cookie != ''){
- fputs($fp, "Cookie: $cookie/n/n");
- }
- fputs($fp, "Content-type: application/x-www-form-urlencoded/n");
- fputs($fp, "Content-length: " . strlen($post) . "/n");
- fputs($fp, "Connection: close/n/n");
- fputs($fp, "$post /n");
- $ret = '';
- while (!feof($fp)) {
- $c = fgets($fp, 1024);
- if (preg_match("/^HTTP/", $c) || preg_match("/^P3P/", $c) || preg_match("/^([A-Za-z/-]+)/:/s*(.*)$/", $c)){
- continue;
- }
- if (trim($c) == ""){
- continue;
- }
- $ret .= $c;
- }
- fclose($fp);
- return $ret;
- }
- }
- [小技巧] 【使用Curl组件GET/POST数据】
- curl get post 请求数据
- curl get post 获取数据
- CURL 使用小技巧
- php 利用curl get post数据
- 使用curl命令post数据
- php使用curl可以get 模拟post
- 使用curl 命令模拟POST/GET请求
- PHP使用CURL模拟POST/GET方式提交数据,支持HTTPS请求
- PHP使用CURL模拟POST/GET/PUT/DELETE方式提交数据
- PHP 使用curl库来发送GET,POST请求,处理json格式数据
- curl post and get
- CURL模拟POST/GET
- cURL-GET/POST/上传
- CURL GET POST
- php curl (get post)
- curl 常用命令 post、get
- php curl post get
- WinCE中支持多国语言切换(zhuan)
- 分手后还能再做朋友吗? (转)
- const用法小记
- 在myeclipse中配置weblogic
- 如何远程控制局域网中的电脑
- [小技巧] 【使用Curl组件GET/POST数据】
- Web聊天:八个优秀的网页聊天服务
- 第一个opengl程序完整攻略
- 限制"域管理员"在本机权限的技巧
- 关于postscript
- 《SQL数据库基础与应用》复习题
- 嵌套for循环的使用
- 一次局域网入侵全过程 2008-10-19 10:53:53 www.hackbase.com 来源:互联网
- 使用 jQuery 简化 Ajax 开发