CurlClient.php 1004 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. namespace Moinax\TvDb\Http;
  3. use Moinax\TvDb\CurlException;
  4. class CurlClient implements HttpClient
  5. {
  6. /**
  7. * Fetch the given url contents using cURL library
  8. *
  9. * @see \Moinax\TvDb\Http\HttpClient::fetch()
  10. */
  11. public function fetch($url, array $params = array(), $method = HttpClient::GET)
  12. {
  13. $ch = curl_init($url);
  14. curl_setopt($ch, CURLOPT_HEADER, 1);
  15. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  16. if ($method == HttpClient::POST) {
  17. curl_setopt($ch, CURLOPT_POST, 1);
  18. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  19. }
  20. $response = curl_exec($ch);
  21. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  22. $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
  23. $data = substr($response, $headerSize);
  24. curl_close($ch);
  25. if ($httpCode != 200) {
  26. throw new CurlException(sprintf('Cannot fetch %s', $url), $httpCode);
  27. }
  28. return $data;
  29. }
  30. }