search
Homephp教程php手册PHP Request工具类

PHP Request工具类

Jun 06, 2016 pm 07:35 PM
httpphprequestusetoolconduct

利用PHP进行HTTP请求是很常见的事情了,大家都知道用CURL来进行操作,但是CURL函数使用起来配置是很多的,很多人往往只有用到一小部分常用选项,这里我做了一个封装 主要功能 POST,GET COOKIE支持 URLENCODE支持(针对GET请求) 由于是静态方法,直接::调用即

利用PHP进行HTTP请求是很常见的事情了,大家都知道用CURL来进行操作,但是CURL函数使用起来配置是很多的,很多人往往只有用到一小部分常用选项,这里我做了一个封装
主要功能
POST,GET
COOKIE 支持
URLENCODE 支持(针对GET请求)

由于是静态方法,直接 :: 调用即可
<?php

/**
 * @version 1.0
 * @date 2014-08-11
 * @author 十七号 <xialeistudio@gmail.com>
 * @license MIT
 * Class Request 网络工具类
 */
class Request
{
	/**
	 * HTTP GET 请求
	 * @param string $url 请求地址
	 * @param array $data 请求数据
	 * @param null $cookie COOKIE
	 * @param null $cookiefile COOKIE 请求所用的COOKIE文件位置
	 * @param null $cookiesavepath 请求完成的COOKIE保存位置
	 * @param bool $encode 是否对请求参数进行 urlencode 处理
	 * @return mixed
	 * @throws Exception
	 */
	public static function get($url, $data = array(), $cookie = null, $cookiefile = null,$cookiesavepath = null, $encode = true)
	{
		//初始化句柄
		$ch = curl_init();
		//处理GET参数
		if(count($data)>0){
			$query = $encode?http_build_query($data):urldecode(http_build_query($data));
			curl_setopt($ch, CURLOPT_URL, $url . '?' . $query);
		}else{
			curl_setopt($ch, CURLOPT_URL, $url);
		}
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36');
		//设置cookie
		if (isset($cookie)) curl_setopt($ch, CURLOPT_COOKIE, $cookie);
		//设置cookie请求文件
		if (isset($cookiefile)){
			if(!is_file($cookiefile)) throw new Exception('Cookie文件不存在');
			curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
		}
		//设置cookie保存路径
		if(isset($cookiesavepath)) curl_setopt($ch,CURLOPT_COOKIEJAR,$cookiesavepath);
		//执行请求
		$resp = curl_exec($ch);
		//关闭句柄,释放资源
		curl_close($ch);
		return $resp;
	}

	/**
	 * HTTP POST 请求
	 * @param string $url 请求地址
	 * @param array $data 请求数据
	 * @param null $cookie 请求COOKIE
	 * @param null $cookiefile 请求时cookie文件位置
	 * @param null $cookiesavepath 请求完成的COOKIE保存位置
	 * @return string
	 * @throws Exception
	 */
	public static function post($url, $data = array(), $cookie = null, $cookiefile = null,$cookiesavepath = null)
	{
		//初始化请求句柄
		$ch = curl_init();
		//参数设置
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch,CURLOPT_POST,1);
		curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
		curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36');
		//cookie设置
		if (isset($cookie)) curl_setopt($ch, CURLOPT_COOKIE, $cookie);
		//请求cookie设置
		if (isset($cookiefile)){
			if(!is_file($cookiefile)) throw new Exception('Cookie文件不存在');
			curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
		}
		//设置cookie保存路径
		if(isset($cookiesavepath)) curl_setopt($ch,CURLOPT_COOKIEJAR,$cookiesavepath);
		$resp=curl_exec($ch);
		curl_close($ch);
		return $resp;
	}
}
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.