基于cURL的HTTP客户端 - 简单但有效的OOP包装器,围绕Curl php库

v2.0.4 2016-06-24 20:27 UTC

This package is auto-updated.

Last update: 2024-09-08 22:01:01 UTC


README

因为cURL不够简单...

包含了一个尽可能简单的RequestResponse对象,用于通过cURL API进行HTTP请求。

你可以得到代码!

如何完成任务...

制作一个简单的GET请求

require_once("vendor/autoload.php");

use DatingVIP\cURL\Request;
use DatingVIP\cURL\Response;

try {
	$response = (new Request())
		->setHeadersUsed(true)
		->get("http://www.example.com");
} catch (\RuntimeException $ex) {
	echo (string) $ex;
} finally {
	printf("Got %d bytes from %s in %.3f seconds\n",
		strlen((string)$response),
		$response->getURL(),
		$response->getTime());
}

制作一个POST请求

require_once("vendor/autoload.php");

use DatingVIP\cURL\Request;
use DatingVIP\cURL\Response;

try {
	$response = (new Request())
		->setHeadersUsed(true)
		->post("http://www.example.com", ["hello" => "world"]);
} catch (\RuntimeException $ex) {
	echo (string) $ex;
} finally {
	printf("Posted %d bytes from %s in %.3f seconds\n",
		strlen((string)$response),
		$response->getURL(),
		$response->getTime());
}

如果你喜欢无理由地对所有事情都进行超级详细的描述

require_once("vendor/autoload.php");

use DatingVIP\cURL\Request;
use DatingVIP\cURL\Response;

try {
	$request = new Request([
		CURLOPT_HTTPHEADER => [
			"x-my-header" => "x-my-value"
		],
		CURLOPT_URL => "http://www.example.com"
	]);

	$response = new Response($request);
} catch (\RuntimeException $ex) {
	echo (string) $ex;
} finally {
	if ($response instanceof Response) {
		printf("Got %d bytes from %s in %.3f seconds\n",
			strlen((string)$response),
			$response->getURL(),
			$response->getTime());
	}
}