note 88473 deleted from book.curl by googleguy
| From: | googleguy@php.net | Date: | Tue, 22 Oct 2013 08:03:49 +0000 |
| Subject: | note 88473 deleted from book.curl by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-196782@lists.php.net to get a copy of this message | ||
Note Submitter: web at davss dot com
----
Here is a reusable curl object - please help me improve it. There is no erro handling (I don't
need it) which can be implemented
<?php
class cURL {
# defaul global options
var $opts = array(
CURLOPT_HEADER => FALSE,
CURLOPT_RETURNTRANSFER => TRUE
);
function cURL(){
}
function r($ch,$opt){
# assign global options array
$opts = $this->opts;
# assign user's options
foreach($opt as $k=>$v){$opts[$k] = $v;}
curl_setopt_array($ch,$opts);
curl_exec($ch);
$r['code'] = curl_getinfo($ch,CURLINFO_HTTP_CODE);
$r['cr'] = curl_exec($ch);
$r['ce'] = curl_errno($ch);
curl_close($ch);
return $r;
}
function get($url='',$opt=array()){
# create cURL resource
$ch = curl_init($url);
return $this->r($ch,$opt);
}
function post($url='',$data=array(),$opt=array()){
# set POST options
$opts[CURLOPT_POST] = TRUE;
$opts[CURLOPT_POSTFIELDS] = $data;
# create cURL resource
$ch = curl_init($url);
return $this->r($ch,$opt);
}
};
$cURL = new cURL();
#
# END of CLASS
#
/*
* test.php files for testing purposes - must be in the same folder undelss you specify $url path
*
<?php
echo " WORKING!!<p>";
if($_GET > 0){
print_r($_GET);
}
elseif($_POST > 0){
print_r($_POST);
}
?>
*/
/*
# EXAMPLE OF USE
$path = "http://"; # path to test.php file
$file = "test.php"; # safe the above code in text.php file
$url = $path . $file;
$post_data = array("FLD1"=>"VAL1","FLD2"=>"VAL2");
# user's curl options that override global if necessary
$o = array(CURLOPT_HEADER => TRUE,CURLOPT_RETURNTRANSFER =>
TRUE,CURLOPT_FAILONERROR=>TRUE);
function ccc($arr){
echo "CODE: " . $arr['code'] . "<p>";
echo "CE: " . $arr['ce'] . "<p>";
echo "CR: <br />" . $arr['cr'] . "<p>";
}
#POST TEST
$cPost = $cURL->post($url,$post_data,$o); // you can remove options
echo "<h2>cPost</h2>";
ccc($cPost);
#GET TEST
$cGet = $cURL->post($url,$post_data,$o); // you can remove options
echo "<h2>cGet</h2>";
ccc($cGet);
*/
?>