Skip to content

Instantly share code, notes, and snippets.

@chadgh
Created August 24, 2010 17:46
Show Gist options
  • Save chadgh/547959 to your computer and use it in GitHub Desktop.
Save chadgh/547959 to your computer and use it in GitHub Desktop.
A solution to simplify the use of curl in php
<?php
/**
* @name Curling.class.php
*
* Purpose: encapsulates and simplifies curl functionality
*
* @author Chad G. Hansen
*/
class Curling
{
/**
* @name sendGET
* @param $url - the url where the request should be sent.
* @param $params - a query string without the question mark (?).
* @param $return - true or false indicating whether you are expecting something back from the request.
* @return the html or string that is returned from the desired web site, or
* a plain text string explaining the error that occured.
*/
public static function sendGET($url, $params = "", $return = true)
{
$params = trim($params);
$conn = curl_init();
curl_setopt($conn, CURLOPT_URL, $url . ($params == "" ? "" : "?") . ($params == "" ? "" : $params));
curl_setopt($conn, CURLOPT_HEADER, false);
curl_setopt($conn, CURLOPT_RETURNTRANSFER, $return);
//curl_setopt($conn, CURLOPT_FOLLOWLOCATION, true);
if (($rtn = curl_exec($conn)) === false)
{
$rtn = curl_error($conn);
}
$code = curl_getinfo($conn, CURLINFO_HTTP_CODE);
curl_close($conn);
return array($code, $rtn);
}
/**
* @name sendPOST
* @param $url - the url where the request should be sent.
* @param $params - a query string without the question mark (?).
* @param $return - true or false indicating whether you are expecting something back from the request.
* @return the html or string that is returned from the desired web site, or
* a plain text string explaining the error that occured.
*/
public static function sendPOST($url, $params = "", $return = true)
{
$params = trim($params);
$conn = curl_init();
curl_setopt($conn, CURLOPT_URL, $url);
curl_setopt($conn, CURLOPT_HEADER, false);
curl_setopt($conn, CURLOPT_RETURNTRANSFER, $return);
curl_setopt($conn, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($conn, CURLOPT_POST, true);
curl_setopt($conn, CURLOPT_POSTFIELDS, $params);
if (($rtn = curl_exec($conn)) === false)
{
$rtn = curl_error($conn);
}
$code = curl_getinfo($conn, CURLINFO_HTTP_CODE);
curl_close($conn);
return array($code, $rtn);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment