Last active
August 29, 2015 14:05
-
-
Save commadelimited/ffe80b098d28a47fdae2 to your computer and use it in GitHub Desktop.
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
// An existing code sample I have uses cURL (found below). | |
// But is there a reason why someone would use the second example instead of the first example? | |
// They both appear to do the exact same thing, but the first one is MUCH simpler. | |
// Thoughts? | |
<?php | |
$response = file_get_contents("http://feeds.arstechnica.com/arstechnica/index?format=xml"); | |
print_r($response); | |
?> | |
<?PHP // is cURL installed yet? | |
if (!function_exists('curl_init')){ | |
die('Sorry cURL is not installed!'); | |
} | |
// OK cool - then let's create a new cURL resource handle | |
$ch = curl_init(); | |
// Now set some options (most are optional) | |
// Set URL to download | |
curl_setopt($ch, CURLOPT_URL, "http://feeds.arstechnica.com/arstechnica/index?format=xml"); | |
// Set a referer | |
curl_setopt($ch, CURLOPT_REFERER, "http://roughlybrilliant.com/chapter5"); | |
// User agent | |
curl_setopt($ch, CURLOPT_USERAGENT, "RoughlyBrillantCurl/1.0"); | |
// Include header in result? (0 = yes, 1 = no) | |
curl_setopt($ch, CURLOPT_HEADER, 0); | |
// Should cURL return or print out the data? (true = return, false = print) | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
// Timeout in seconds | |
curl_setopt($ch, CURLOPT_TIMEOUT, 10); | |
// Download the given URL, and return output | |
$output = curl_exec($ch); | |
// Close the cURL resource, and free system resources | |
curl_close($ch); | |
echo $output; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You'll find that for a lot of APIs (Paypay - or other payment gateways, Twitter, etc.), you'll need more extensive configurations for your requests - hence cURL.
file_get_contents() is super simple, but less configurable. I use it to scrape sites for simple things, but when you need to do a PUT or DELETE, etc - cURL is better suited for that.
Both include a sort of authentication header, as well. Diff tools for diff needs, basically.