aicial / webrequest
提供GET、PUT、POST、HEAD、Cookies等功能,用于file_get_contents的Web请求库
0.1.7
2016-02-07 00:46 UTC
Requires
- php: >=5.3.0
Requires (Dev)
- phpunit/phpunit: 4.0.*
This package is not auto-updated.
Last update: 2020-07-30 16:02:30 UTC
README
提供了一种简单的方式与Web服务交互(目前使用file_get_contents())。轻松跟踪交换的Cookies,便于抓取。以下是一个Thirdlane示例。
安装
cd <project_dir>
composer require aicial\webrequest
用法
require __DIR__ . '/vendor/autoload.php'; //Use composers autoload
$webRequest = new Aicial\Webrequest\Webrequest();
$webRequest->setURL('https://YOUR_SERVICE.TLD/');
$webRequest->setJSON(true);
$serverResponse = $webRequest->go();
var_dump($serverResponse);
方法
go() //Perform the web request
setCookies() //Set any cookies to be included in the headers
setHeaders() //Set any headers to be sent to the target URL
setJSON() //Is this transaction sending / expecting JSON
setJSONPretty() //Should JSON being sent be encoded in an easily readable format?
setMethod() //Set the HTTP method GET, HEAD, PUT, POST are valid
setPost() //Set post contents
setQuerystring() //Set querystring data
setTrackCookies() //Should cookies be tracked?
setURL() //Set the target URL Must include http:// or https://
setVerifyPeer() //Should SSL peers be verified?
setWebTimeout() //Set's the value that determines how long we wait for a response
示例 - 使用Cookies
交换的Cookies跟踪在包内部处理。您只需要打开它,就可以与服务器进行简单的抓取交易。以下是从ThirdLane PBX服务器获取记录调用列表的示例
<?php
require __DIR__ . '/vendor/autoload.php'; //Use composers autoload
$webrequest = new Aicial\Webrequest\Webrequest();
$url = 'http://mypbx.server:10000';
$user = 'mytlusername';
$password = 'mytlpassword';
$loginUrl = 'session_login.cgi';
$recordedUrl = 'asterisk/recorded_calls.cgi';
$webrequest->setURL($url.'/'.$loginUrl);
$webrequest->setVerifyPeer(false); //Most Thirdlane servers have invalid SSL
$webrequest->setTrackCookies(true); //Return cookies received
$response = $webrequest->go(); //Make the initial GET request
$post = array('user' => $this->config['username'], 'pass' => $this->config['password'], 'page' => '/', 'save' => '1');
$webrequest->setMethod('POST'); //Use a form POST
$webrequest->setPost($post); //Supply the POST data
$response = $webrequest->go(); //Notice the URL has not been set again - it is still set from before.
$webrequest->setURL($url.'/'.$recordedUrl); //The URL for Recorded Calls Listing
$webrequest->setMethod('GET'); //Back to using HTTP GET
$webrequest->setPost(); //Clear the POST array
$response = $webrequest->go(); //Get the page data
print_r($response); //Do what you want with the content
注意
尽管该包支持Cookies,但它不遵守域或过期(目前如此)。
发送服务器无法使Cookies过期(删除)。一些与服务器的交互需要此功能以进行权限提升。如果您的这种情况,目前唯一的解决方法是知道何时删除Cookies,并在代码中执行。
历史
2016年2月4日 - 首次公开发布,添加了示例代码
2016年2月5日 - 修复了无法设置HTTP超时的缺失功能,增加了关于限制的说明
版权
2016年 Troy Kelly,Aicial Pty Ltd(澳大利亚)