Plinker RPC Documentation v0.0.1
Class Plinker Lib

Curl

  1. /*
  2. +------------------------------------------------------------------------+
  3. | Plinker PHP Extension |
  4. +------------------------------------------------------------------------+
  5. | Copyright (c)2017-2017 (https://github.com/plinker-rpc/php-ext) |
  6. +------------------------------------------------------------------------+
  7. | This source file is subject to GNU General Public License v2.0 License |
  8. | that is bundled with this package in the file LICENSE. |
  9. | |
  10. | If you did not receive a copy of the license and are unable to |
  11. | obtain it through the world-wide-web, please send an email |
  12. | to license@cherone.co.uk so we can send you a copy immediately. |
  13. +------------------------------------------------------------------------+
  14. | Authors: Lawrence Cherone |
  15. +------------------------------------------------------------------------+
  16. */
  17. namespace Plinker\Lib;
  18. final class Curl
  19. {
  20. private config;
  21. private options;
  22. /**
  23. *
  24. */
  25. public function __construct(array! config = []) -> void
  26. {
  27. //
  28. let this->config = config;
  29. }
  30. final private function setOptions() -> void
  31. {
  32. //
  33. let this->options = [
  34. CURLOPT_FAILONERROR : true,
  35. CURLOPT_FOLLOWLOCATION : true,
  36. CURLOPT_RETURNTRANSFER : true,
  37. CURLOPT_SSL_VERIFYPEER : false,
  38. CURLOPT_SSL_VERIFYHOST : false,
  39. CURLOPT_ENCODING : "gzip",
  40. CURLOPT_HTTPHEADER : [
  41. "Content-Type: application/json"
  42. ]
  43. ];
  44. }
  45. /**
  46. * POST
  47. */
  48. public function post(string! url, var parameters = null, array! headers = []) -> string
  49. {
  50. var curl, body;
  51. //
  52. this->setOptions();
  53. //
  54. if is_array(parameters) {
  55. let parameters = json_encode(parameters);
  56. let parameters = gzdeflate(parameters, 9);
  57. }
  58. //
  59. let curl = curl_init(url);
  60. //
  61. let this->options[CURLOPT_POST] = true;
  62. let this->options[CURLOPT_POSTFIELDS] = parameters;
  63. //
  64. if !empty headers {
  65. var header; for header in headers {
  66. let this->options[CURLOPT_HTTPHEADER][] = header;
  67. }
  68. }
  69. //
  70. curl_setopt_array(curl, this->options);
  71. //
  72. let body = curl_exec(curl);
  73. if curl_error(curl) {
  74. return serialize([
  75. "url" : url,
  76. "error" : curl_error(curl),
  77. "code" : curl_getinfo(curl, CURLINFO_HTTP_CODE)
  78. ]);
  79. }
  80. //
  81. curl_close(curl);
  82. //
  83. return body;
  84. }
  85. }