Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ciptard/1f4a4bfd54f87e9d1230 to your computer and use it in GitHub Desktop.
Save ciptard/1f4a4bfd54f87e9d1230 to your computer and use it in GitHub Desktop.
<?php
function fetch($url, $cookies = null, $returnCookie = false) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if($cookies){
curl_setopt($ch, CURLOPT_COOKIE, implode(';',$cookies));
}
curl_setopt($ch, CURLOPT_HEADER, 1);
$result = curl_exec($ch);
list($header, $body) = explode("\r\n\r\n", $result, 2);
$end = strpos($header, 'Content-Type');
$start = strpos($header, 'Set-Cookie');
$parts = explode('Set-Cookie:', substr($header, $start, $end - $start));
$cookies = array();
foreach ($parts as $co) {
$cd = explode(';', $co);
if (!empty($cd[0]))
$cookies[] = $cd[0];
}
curl_close($ch);
if ($returnCookie){
return $cookies;
}
return json_decode($body);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment