Skip to content

Instantly share code, notes, and snippets.

@Pamblam
Last active February 8, 2018 20:23
Show Gist options
  • Save Pamblam/1919100f017e27548a85ee695120a3a4 to your computer and use it in GitHub Desktop.
Save Pamblam/1919100f017e27548a85ee695120a3a4 to your computer and use it in GitHub Desktop.
<?php
/*
* Makes an HTTP request via GET or POST, and can download a file
* @returns - Returns the response of the request
* @param $url - The URL to request, including any GET parameters
* @param $params - An array of POST values to send
* @param $filename - If provided, the response will be saved to the
* specified filename
*/
function request($url, $params = array(), $filename = ""){
$ch = curl_init();
$curlOpts = array(
CURLOPT_URL=>$url,
// Set Useragent
CURLOPT_USERAGENT=>
'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0',
// Don't validate SSL
// This is to prevent possible errors with self-signed certs
CURLOPT_SSL_VERIFYPEER=>false,
CURLOPT_SSL_VERIFYHOST=>false,
CURLOPT_RETURNTRANSFER=>true,
CURLOPT_FOLLOWLOCATION=>true
);
if(!empty($filename)){
// If $filename exists, save content to file
$file2 = fopen($filename, 'w+')
or die("Error[" . __FILE__ . ":" . __LINE__ . "]
Could not open file: $filename");
$curlOpts[CURLOPT_FILE] = $file2;
}
if(!empty($params)){
// If POST values are given, send that shit too
$curlOpts[CURLOPT_POST] = true;
$curlOpts[CURLOPT_POSTFIELDS] = $params;
}
curl_setopt_array($ch, $curlOpts);
$answer = curl_exec($ch);
// If there was an error, show it
if(curl_error($ch)) die(curl_error($ch));
if(!empty($filename)) fclose($file2);
curl_close($ch);
return $answer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment