note 88473 added to book.curl
| From: | web at davss dot com | Date: | Mon, 26 Jan 2009 10:00:51 +0000 |
| Subject: | note 88473 added to book.curl | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-149771@lists.php.net to get a copy of this message | ||
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);
*/
?>
----
Server IP: 69.147.83.197
Probable Submitter: 198.54.202.214
----
Manual Page -- http://www.php.net/manual/en/book.curl.php
Edit -- https://master.php.net/note/edit/88473
Del: integrated -- https://master.php.net/note/delete/88473/integrated
Del: useless -- https://master.php.net/note/delete/88473/useless
Del: bad code -- https://master.php.net/note/delete/88473/bad+code
Del: spam -- https://master.php.net/note/delete/88473/spam
Del: non-english -- https://master.php.net/note/delete/88473/non-english
Del: in docs -- https://master.php.net/note/delete/88473/in+docs
Del: other reasons-- https://master.php.net/note/delete/88473
Reject -- https://master.php.net/note/reject/88473
Search -- https://master.php.net/manage/user-notes.php