Created
August 24, 2010 17:46
-
-
Save chadgh/547959 to your computer and use it in GitHub Desktop.
A solution to simplify the use of curl in php
This file contains hidden or 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
<?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