PHPHttpRequest is a bunch of classes that are similar to JavaScript XMLHttpRequest.
- PHP > 5.3.0
- Fileinfo functions are not disabled
- Network functions are not disabled
- URLs functions are not disabled
All codes are running in PHP.
You must require all files to use PHPHttpRequest, including File.class.php, FormData.class.php, PHPHttpResponse.class.php and PHPHttpRequest.class.php.
You can uncomment namespace PHPHttpRequest; at start of all files to use namespace, this can avoid conflict from other packages.
-
Create a File instance use file path.
$file = new File('/path/to/file'); -
Use properties
mimetype,filesize,filenameandfilepathto get file MIME type, size, name and path.Notice: file that larger than 2GB may fail to get filesize.
$file->mimetype; //file MIME type $file->filesize; //file size $file->filename; //file name $file->filepath; //file path -
File content can be get as string, can be sliced.
$file->readAsString(); //get as string $file->readAsString(5); //get as string from the 6th byte (slice off the starting 5 bytes) $file->readAsString(5, 30); //get as string from the 6th byte, and 30 bytes as mostOr as data URI.
$file->readAsDataURI(); //get as data URI
FormData store data like HTML form.
-
Create a FormData instance.
$fd = new FormData(); -
Use
appendmethod to add name and value to data, those are similar tonameandvalueattribute of HTML elementinput,select,textarea, etc.$fd->append('somename', 'somevalue'); //`name="somename"` and `value="somevalue"You can add File instance as value. You can override file name use a third parameter.
$fd->append('file', $file); //use File instance $fd->append('anotherfile', $file, 'bettername') //override file nameNotice: data won't be overwrite when using the same name. All of them will be stored.
-
You can set property
multiparttotrue, which can tell PHPHttpRequest to send FormData as MIME typemultipart/form-data, or it will send with proper type (application/x-www-form-urlencodedwhen no File, ormultipart/form-datawhen having File).$fd->multipart = true; //force to use `multipart/form-data`You can get properties
multipartandhasFileto check;$fd->multipart; //default to `false` $fd->hasFile; //`true` when having File -
You can get data using
shift()method from start, or usingpop()method from end.$one = $fd->shift(); //from start $another = $fd->pop(); //from endYou can
reset()to get data again.$fd->reset(); //reset
Use PHPHttpRequest is very similar to JavaScript XMLHttpRequest. Only difference is the way to get response data.
-
Create a PHPHttpRequest instance.
$phr = new PHPHttpRequest(); -
You only need two parameters, method and URL.
$phr->open('post', 'http://example.com/');method can be
head,get,post,put,delete. More is WIP.URL should be absolute path with leading
http(s)://. Not support basic authentication at present. -
You can set your own HTTP header, with name and value pair.
$phr->setRequestHeader('Header-Name', 'Header-Value');Notice: header will be overwrite with the same name. Notice:
Content-Typewill be override when sending FormData or File.Content-Lengthwill be override all the time.Cookiewill be ignored all the time, usesetCookie(). -
You can set request cookies with name and value.
$phr->setCookie('cookiename', 'cookievalue');Notice: cookie will be overwrite with the same name.
-
You can send data like this:
$phr->send($data);$datacan be FormData instance, File instance, string, or just nothing.FormData will be sent like HTML form.
File will be sent originally as binary string, and set header
Content-Typeautomatically.String will be sent originally, you can send every thing that can store in string.
Notice: once sent, every parameter that set will be reset, you must set them again from step 2, or you won't send anything again.
-
You can access response data using
responseproperty, which is a PHPHttpResponse instance. See more introductions below.
-
You don't need to create instance yourself. after successfully sent data using PHPHttpResquest, you can access like this:
$phr->response; -
You can get status code using
statusproperty.$phr->response->status; -
You can get HTTP headers using
getHeader()method.$phr->response->getHeader(); //get all headers $phr->response->getHeader('Header-Name') //get headers having the nameAn array will be returned, which contains at least one array, with
nameandvalueelements.falsereturned when no matching.Notice:
Set-Cookiecan not be get using this. UsegetCookie()instead. -
You can get cookies using
getCookie()method.$phr->response->getCookie(); //get all cookies $phr->response->getCookie('cookiename') //get cookies having the nameAn array will be returned, which contains at least one array, with elements
name,value,expires(in timestamp),path,domain,secure(boolean) orhttponly(boolean). Not all elements exists. -
You can get response body like this:
$phr->response->data;
This is original data. You must parse it yourself.
You can pay me some money to support my works. It's on your own.