CurlService.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php namespace Ixudra\Curl;
  2. class CurlService {
  3. public function get($url, $getParameters = array(), $isJson = false, $curlOptions = array())
  4. {
  5. $curl = new Curl();
  6. $curl->setUrl( $url, $getParameters );
  7. foreach( $curlOptions as $key => $value ) {
  8. $curl->addOption( $key, $value );
  9. }
  10. return $this->send( $curl, $isJson );
  11. }
  12. public function post($url, $getParameters = array(), $postParameters, $isJson = false, $curlOptions = array())
  13. {
  14. $curl = new Curl();
  15. $curl->setUrl( $url, $getParameters );
  16. $curl->setMethod( true );
  17. if( $isJson ) {
  18. $postParameters = json_encode($postParameters);
  19. $curl->addOption( 'HTTP_HEADER', array('Content-Type: application/json') );
  20. }
  21. $curl->setPostParameters( $postParameters );
  22. foreach( $curlOptions as $key => $value ) {
  23. $curl->addOption( $key, $value );
  24. }
  25. return $this->send( $curl, $isJson );
  26. }
  27. protected function send($curl, $isJson)
  28. {
  29. $response = $curl->send();
  30. if( $isJson ) {
  31. $response = json_decode($response);
  32. }
  33. return $response;
  34. }
  35. }