Last active
October 7, 2015 13:38
-
-
Save Idered/3173259 to your computer and use it in GitHub Desktop.
curlQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Pobiera dane ze wskazanego adresu URL poprzez cURL. | |
* | |
* @todo Ta funkcja jest zbyt ogólna, by znajdować się w komendach czata. | |
* @param string $url Adres URL. | |
* @param array $headers Nagłówki, jakie należy przesłać razem z zapytaniem. | |
* @param bool $isPost Informacja, czy zapytanie ma zostać zadane metodą POST. Jeśli false, będzie użytwa metoda GET. | |
* @param mixed $data Dane do przesłania metodą post. | |
* @return string | |
*/ | |
private function curlQuery($url, array $headers = array(), $isPost = false, $data = array()) | |
{ | |
$connection = curl_init(); | |
curl_setopt_array($connection, array( | |
CURLOPT_USERAGENT => 'Graffika.pl cURL', | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_URL => $url, | |
CURLOPT_TIMEOUT => 7, | |
CURLOPT_SSL_VERIFYPEER => false, | |
CURLOPT_SSL_VERIFYHOST => 2, | |
CURLOPT_SSLVERSION => 3 | |
)); | |
if (!empty($headers)) { | |
curl_setopt($connection, CURLOPT_HTTPHEADER, $headers); | |
} | |
if ($isPost) { | |
curl_setopt($connection, CURLOPT_POST, true); | |
} else { | |
curl_setopt($connection, CURLOPT_HTTPGET, true); | |
} | |
if ($isPost && !empty($data)) { | |
curl_setopt($connection, CURLOPT_POSTFIELDS, $data); | |
} | |
$result = curl_exec($connection); | |
if (curl_errno($connection) > 0) { | |
return false; | |
} | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment