项目作者: jabranr

项目描述 :
Simple PHP client for cURL operations
高级语言: PHP
项目地址: git://github.com/jabranr/php-curl.git
创建时间: 2016-10-09T23:15:57Z
项目社区:https://github.com/jabranr/php-curl

开源协议:MIT License

下载


PHP cURL Build Status Latest Stable Version Total Downloads

A simple PHP client for cURL operations.

Migrating from v1? Beware that v2 has breaking changes! Some of the API methods names have changed.

Simply import the library into your project. The best way to do so is to use Composer as following. Otherwise it can simply be downloaded from GitHub and added to the project.

  1. $ composer require jabranr/php-curl

Start using it straight away. (Following example assumes that it was installed via Composer)

  1. <?php
  2. require 'path/to/vendor/autoload.php';
  3. use Jabran\HttpUtil\HttpCurlRequest;
  4. use Jabran\HttpUtil\Exception\HttpCurlException;
  5. # Start new cURL request
  6. $curl = new HttpCurlRequest('http://jabran.me');

Setting cURL options

  1. <?php
  2. # Set options - method 1
  3. $curl->setOption(CURLOPT_RETURNTRANSFER, true);
  4. $curl->setOption(CURLOPT_FOLLOWLOCATION, true);
  5. # Set options - method 2
  6. $curl->setOptions(array(
  7. CURLOPT_RETURNTRANSFER => true,
  8. CURLOPT_FOLLOWLOCATION => true
  9. ));
  10. # Execute request, get response and close connection
  11. try {
  12. $response = $curl->execute();
  13. } catch(HttpCurlException $e) {
  14. $curl->getErrorCode(); // -> cURL error number
  15. $curl->getErrorMessage(); // -> cURL error message
  16. # OR
  17. $e->getMessage(); // -> cURL error: [ErrorCode] ErrorMessage
  18. }
  19. # OR get more info and close connection manually
  20. try {
  21. $response = $curl->execute(false);
  22. } catch(HttpCurlException $e) { }
  23. # Get response later
  24. $response = $curl->getResponse();
  25. #
  26. $info = $curl->getInfo();
  27. # Close request
  28. $curl->close();

API

The cURL class exposes following API:

getInfo

Get cURL request info. $option needs to be a valid cURL constant. If none given then it will return an associative array.

  1. $curl->execute(false);
  2. $curl->getInfo($option = null);
  3. $curl->close();

getError

Get cURL request error message.

  1. $curl->getError();

getErrorCode

Get cURL request error code.

  1. $curl->getErrorCode();

getErrorMessage

Get cURL request error message.

  1. $curl->getErrorMessage();

getHttpCode

Get cURL request HTTP status code.

  1. $curl->getHttpCode();

getTotalTime

Get total time taken for a cURL request.

  1. $curl->getTotalTime();

getResponse

Get response for a cURL request.

  1. $curl->getResponse();

License

Feel free to use and send improvements via pull requests. Licensed under the MIT License.

© Jabran Rafique – 2016 – 2017