Skip to content

Instantly share code, notes, and snippets.

@markandrewj
Last active August 29, 2015 14:05
Show Gist options
  • Save markandrewj/b2b16799cdd3e6ed5b17 to your computer and use it in GitHub Desktop.
Save markandrewj/b2b16799cdd3e6ed5b17 to your computer and use it in GitHub Desktop.
JSON Curl function for PHP
<?php
function curl_json($base_url='',$query='',$json=true){
//set the url to use
$target_url = 'http://www.mydomain.com/json_script.json';
$url == ($base_url='' ? $target_url : $base_url) ;
//set default json
$default_json ='[you json string here]';
$json_query == ($query='' ? $query : $default_json) ;
//http auth
$username = 'your_username'; // authentication
$password = 'your_password'; // authentication
$ch = curl_init();
$headers = array();
if($json) {
$headers[] = 'Content-type: application/json';
$headers[] = 'X-HTTP-Method-Override: GET';
}
$options = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERPWD => $username . ":" . $password, // authentication
CURLOPT_HTTPHEADER => array($headers),
CURLOPT_POSTFIELDS => $json_query,
CURLOPT_TIMEOUT => 5,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HEADER => 0,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_MAXREDIRS => 3,
CURLOPT_USERAGENT => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)'
);
curl_setopt_array($ch,$options);
$response = curl_exec($ch);
if($response === false || curl_error($ch)) {
curl_close($ch);
return false;
} else {
curl_close($ch);
return json_decode($response, true);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment