Here are some simple curl codes I commonly use.
#1 – Fetch url content (basic GET request)
$curl_url = 'http://www.example.com/sample.php'; $ch=curl_init($curl_url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $curl_result = curl_exec($ch); curl_close($ch);
#2 – Fetch url content (basic POST request)
$curl_post_data = array( 'variable_1' => 'data_1', 'variable_2' => 'data_2' ); $curl_url = 'http://www.example.com/sample.php'; $ch = curl_init ($curl_url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($curl_post_data)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $curl_result = curl_exec ($ch); curl_close ($ch); echo $curl_result;
#3 – Curl request with session
#Tips in case the code above is not working
1. The site probably restricts request not sent by common browsers. To imitate a browser, add the curl option below.
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Host: example.com', 'User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:8.0) Gecko/20100101 Firefox/8.0', 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language: en-us,en;q=0.5', 'Connection: keep-alive', ));
2. Youre probably accessing an https site.
<code> curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);</code>
More info regarding this,
http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/
3. Check if there is an error
Place this before closing the curl connection.
$curl_error = curl_error($ch);


