Skip to content

Instantly share code, notes, and snippets.

@romuloctba
Created October 27, 2015 22:17
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 romuloctba/1d16d820d7d5a8ab1ad4 to your computer and use it in GitHub Desktop.
Save romuloctba/1d16d820d7d5a8ab1ad4 to your computer and use it in GitHub Desktop.
php fopen with CURL - using CURL instead of fopen_url()
<?php
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
// now using it:
$url = "https://gravatar.com/romuloctba.json";
$get = get_data($url);
$json = json_decode($get);
print_r($json);
@romuloctba
Copy link
Author

I use this when I get something like:

Warning: fopen() [function.fopen]: http:// wrapper is disabled in the server configuration by allow_url_fopen=0

@romuloctba
Copy link
Author

Obs: It may not work locally if you don't have cURL installed and added to your PATH (eg on windows)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment