salamek / http-request
用于向Web应用程序发送请求的简单类
v1.1
2016-07-06 23:32 UTC
Requires
- php: >=5.3.0
Requires (Dev)
- phpunit/phpunit: 5.3
This package is auto-updated.
Last update: 2024-09-13 05:40:16 UTC
README
一个简单的类,用于作为浏览器执行Web应用程序的请求,在重定向之间存储cookie等
安装
composer require salamek/http-request dev-master
用法
<?php include('vendor/autoload.php'); $httpRequest = new Salamek\HttpRequest('cookie-jar.txt'); $httpResponse = $httpRequest->post('http://example.com/sign/in', ['username' => 'my-username', 'password' => 'my-password']); echo '<pre>'; print_r($httpResponse); print_r($httpResponse->getBody(Salamek\HttpResponse::FORMAT_HTML)); //Xpath echo '</pre>';
文档
/** * HttpRequest constructor. * @param $cookieJar string to store cookies * @param int $maxRedirections redirections allowed (to prevent infinite redirection loop) */ public function __construct($cookieJar, $maxRedirections = 10); /** * Sets maxRedirections * @param int $maxRedirections max redirections allowed (to prevent infinite redirection loop) */ public function setMaxRedirections($maxRedirections); /** * Sends GET Request * @param string $url url to load * @param array $parameters get parameters as array * @return HttpResponse */ public function get($url, array $parameters = []); /** * Sends POST Request * @param string $url to send post request * @param array $parameters post parameters * @return HttpResponse */ public function post($url, array $parameters = []); /** * Sends PUT Request * @param string $url to send put request * @param array $parameters put parameters * @return HttpResponse */ public function put($url, array $parameters = []); /** * Sends DELETE Request * @param string $url to send DELETE request * @param array $parameters DELETE parameters * @return HttpResponse */ public function delete($url, array $parameters = []); /** * Absolutizes url * @param string $baseUrl base url, usualy url of loaded page * @param string $url url to absolutize, usualy url from loaded page * @return string absolutized url */ public static function absolutizeHtmlUrl($baseUrl, $url); /** * Creates file string for file upload * @param string $path path to file * @param string $mime mime type of file * @param string $name send name of file * @return \CURLFile */ public static function createFile($path, $mime, $name);
/** * HttpResponse constructor. * @param $body * @param array $info * @param $lastUrl * @param array $headers */ public function __construct($body, array $info, $lastUrl, array $headers); /** * Returns headers * @return array */ public function getHeaders(); /** * Returns body, formated by $format FORMAT_RAW, FORMAT_JSON, FORMAT_HTML * @param string $format * @return \DOMXPath|mixed */ public function getBody($format = self::FORMAT_RAW); /** * Returns rawBody FORMAT_RAW * @return mixed */ public function getRawBody(); /** * Returns info * @return array */ public function getInfo(); /** * Returns last loded URL * @return mixed */ public function getLastUrl();