<?php
/**
* HTTP 请求类
*/
class HttpHelper { const METHOD_GET = 'GET';
const METHOD_POST = 'POST'; private static $_connectTimeout = 60;
private static $_timeout = 60; /**
* Http post请求
* @param type $url http url address
* @param type $data post params name => value
*/
public static function post($url, $data = array()){ $queryString = self::buildHttpQueryString($data, self::METHOD_POST); $response = self::makeHttpRequest($url, self::METHOD_POST,$queryString);
return $response;
} /**
* http get 请求
* @param type $url http url address
* @param type $data get params name => value
*/
public static function get($url,$data = array()) {
if(!empty($data)){
$url .= "?" . self::buildHttpQueryString($data, self::METHOD_GET);
}
$response = self::makeHttpRequest($url, self::METHOD_GET);
return $response;
} /**
* 构造并发送一个http请求
* @param type $url
* @param type $method
* @param type $postFields
* @return type
*/
public static function makeHttpRequest($url,$method,$postFields = null) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if(self::METHOD_POST == $method){
curl_setopt($ch, CURLOPT_POST, 1);
if(!empty($postFields)){
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
}
}
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::$_connectTimeout);
curl_setopt($ch, CURLOPT_TIMEOUT, self::$_timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $result = curl_exec($ch);
curl_close($ch);
return $result;
} /**
* 构造http请求的查询字符串
* @param array $params
* @param type $method
* @return string
*/
public static function buildHttpQueryString(array $params, $method = self::METHOD_GET) {
if(empty($params)){
return '';
}
if(self::METHOD_GET == $method){
$keys = array_keys($params);
$values = self::urlEncode(array_values($params));
$params = array_combine($keys, $values);
} $fields = array(); foreach ($params as $key => $value) {
$fields[] = $key . '=' . $value;
}
return implode('&',$fields);
} /**
* url encode 函数
* @param type $item 数组或者字符串类型
* @return type
*/
public static function urlEncode($item) {
if(is_array($item)){
return array_map(array('HttpHelper','urlEncode'), $item);
}
return rawurlencode($item);
} /**
* url decode 函数
* @param type $item 数组或者字符串类型
* @return type
*/
public static function urlDecode($item){
if(is_array($item)){
return array_map(array('HttpHelper','urlDecode'), $item);
}
return rawurldecode($item);
}
}

使用方法:

echo HttpHelper::get('http://www.baidu.com', array('t' => 1));

PHP post & get请求的更多相关文章

  1. Angular2入门系列教程7-HTTP(一)-使用Angular2自带的http进行网络请求

    上一篇:Angular2入门系列教程6-路由(二)-使用多层级路由并在在路由中传递复杂参数 感觉这篇不是很好写,因为涉及到网络请求,如果采用真实的网络请求,这个例子大家拿到手估计还要自己写一个web ...

  2. Android请求网络共通类——Hi_博客 Android App 开发笔记

    今天 ,来分享一下 ,一个博客App的开发过程,以前也没开发过这种类型App 的经验,求大神们轻点喷. 首先我们要创建一个Andriod 项目 因为要从网络请求数据所以我们先来一个请求网络的共通类. ...

  3. 重温Http协议--请求报文和响应报文

    http协议是位于应用层的协议,我们在日常浏览网页比如在导航网站请求百度首页的时候,会先通过http协议把请求做一个类似于编码的工作,发送给百度的服务器,然后在百度服务器响应请求时把相应的内容再通过h ...

  4. Taurus.MVC 2.2 开源发布:WebAPI 功能增强(请求跨域及Json转换)

    背景: 1:有用户反馈了关于跨域请求的问题. 2:有用户反馈了参数获取的问题. 3:JsonHelper的增强. 在综合上面的条件下,有了2.2版本的更新,也因此写了此文. 开源地址: https:/ ...

  5. nodejs之get/post请求的几种方式

    最近一段时间在学习前端向服务器发送数据和请求数据,下面总结了一下向服务器发送请求用get和post的几种不同请求方式: 1.用form表单的方法:(1)get方法 前端代码: <form act ...

  6. ajax异步请求

    做前端开发的朋友对于ajax异步更新一定印象深刻,作为刚入坑的小白,今天就和大家一起聊聊关于ajax异步请求的那点事.既然是ajax就少不了jQuery的知识,推荐大家访问www.w3school.c ...

  7. C# MVC 5 - 生命周期(应用程序生命周期&请求生命周期)

    本文是根据网上的文章总结的. 1.介绍 本文讨论ASP.Net MVC框架MVC的请求生命周期. MVC有两个生命周期,一为应用程序生命周期,二为请求生命周期. 2.应用程序生命周期 应用程序生命周期 ...

  8. nodejs进阶(5)—接收请求参数

    1. get请求参数接收 我们简单举一个需要接收参数的例子 如果有个查找功能,查找关键词需要从url里接收,http://localhost:8000/search?keyword=地球.通过前面的进 ...

  9. 无法向会话状态服务器发出会话状态请求。请确保 ASP.NET State Service (ASP.NET 状态服务)已启动,并且客户端端口与服务器端口相同。如果服务器位于远程计算机上,请检查。。。

    异常处理汇总-服 务 器 http://www.cnblogs.com/dunitian/p/4522983.html 无法向会话状态服务器发出会话状态请求.请确保 ASP.NET State Ser ...

  10. [转]利用URLConnection来发送POST和GET请求

    URL的openConnection()方法将返回一个URLConnection对象,该对象表示应用程序和 URL 之间的通信链接.程序可以通过URLConnection实例向该URL发送请求.读取U ...

随机推荐

  1. SqlServer学习笔记【暂】

    Sql学习笔记,暂时先保存在着,等不忙了再整理成章节,如果其中有问题的,还请各位大神不吝赐教! --------------------------------------所有的数据基于Northwi ...

  2. ssh 免密码远程登录

    背景: 公司有两台服务器A与B,经常会碰到代码中的配置文件不一致的情况...............,为了反面让两台服务器配置统一,所以需要写个shell脚本,用到的linux命令主要是scp 1.在 ...

  3. ubuntu17.04安装flash

    因为用不了软件商店(别问我为什么) 所以手动安装 1 下载文件 在firefox下下载  *****.tar.gz 压缩包 ,并解压(一般目录在 /home 当前用户下的 下载目录下) adobe官网 ...

  4. php 过滤掉多维数组空值

    //过滤掉空值 function filter_array($arr, $values = ['',[]]){ foreach ($arr as $k => $v) { if (is_array ...

  5. hdu2328(后缀数组 + 二分)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2328 题意: 求 n 个串的字典序最小的最长公共子串 思路: 本题中单个字符串长度不超过 200, ...

  6. JDK 1.6.0_45 下载

    Java SE Development Kit 6u45 Product / File Description File Size Download password Linux x86 65.46 ...

  7. .NET Services Stack笔记之手写版

  8. kuangbin专题七 ZOJ1610 Count the Colors (灵活线段树)

    Painting some colored segments on a line, some previously painted segments may be covered by some th ...

  9. POJ1049 Microprocessor Simulation

    题目来源:http://poj.org/problem?id=1049 题目大意: 一种小型的微处理器有以下特性: 1. 每个字长4bit. 2. 地址用2个字进行编码.先高位字后低位字,即高位字大的 ...

  10. SwiftMailer 发送邮件时 提示fsockopen() 被禁用

    站点转移空间,发送邮件的SwiftMailer 类提示错误如下: Warning: fsockopen() has been disabled for security reasons in D:\1 ...