我希望有一个独立的 PHP 类,其中有一个函数通过 cURL 调用 API 并获得响应。有人能帮帮我吗?
谢谢。
Just use the below piece of code to get the response from restful web service url, I use social mention url.
$response = get_web_page("http://socialmention.com/search?q=iphone+apps&f=json&t=microblogs&lang=fr"); $resArr = array(); $resArr = json_decode($response); echo "<pre>"; print_r($resArr); echo "</pre>"; function get_web_page($url) { $options = array( CURLOPT_RETURNTRANSFER => true, // return web page CURLOPT_HEADER => false, // don't return headers CURLOPT_FOLLOWLOCATION => true, // follow redirects CURLOPT_MAXREDIRS => 10, // stop after 10 redirects CURLOPT_ENCODING => "", // handle compressed CURLOPT_USERAGENT => "test", // name of client CURLOPT_AUTOREFERER => true, // set referrer on redirect CURLOPT_CONNECTTIMEOUT => 120, // time-out on connect CURLOPT_TIMEOUT => 120, // time-out on response ); $ch = curl_init($url); curl_setopt_array($ch, $options); $content = curl_exec($ch); curl_close($ch); return $content; }
The crux of the solution is setting
CURLOPT_RETURNTRANSFER => true
then
$response = curl_exec($ch);
CURLOPT_RETURNTRANSFER tells PHP to store the response in a variable instead of printing it to the page, so $response will contain your response. Here's your most basic working code (I think, didn't test it):
// init curl object $ch = curl_init(); // define options $optArray = array( CURLOPT_URL => 'http://www.google.com', CURLOPT_RETURNTRANSFER => true ); // apply those options curl_setopt_array($ch, $optArray); // execute request and get response $result = curl_exec($ch);
If anyone else comes across this, I'm adding another answer to provide the response code or other information that might be needed in the "response".
http://php.net/manual/en/function.curl-getinfo.php
// init curl object $ch = curl_init(); // define options $optArray = array( CURLOPT_URL => 'http://www.google.com', CURLOPT_RETURNTRANSFER => true ); // apply those options curl_setopt_array($ch, $optArray); // execute request and get response $result = curl_exec($ch); // also get the error and response code $errors = curl_error($ch); $response = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); var_dump($errors); var_dump($response);
Output:
string(0) "" int(200) // change www.google.com to www.googlebofus.co string(42) "Could not resolve host: www.googlebofus.co" int(0)
The ultimate curl php function:
function getURL($url,$fields=null,$method=null,$file=null){ // author = Ighor Toth <igtoth@gmail.com> // required: // url = include http or https // optionals: // fields = must be array (e.g.: 'field1' => $field1, ...) // method = "GET", "POST" // file = if want to download a file, declare store location and file name (e.g.: /var/www/img.jpg, ...) // please crete 'cookies' dir to store local cookies if neeeded // do not modify below $useragent = 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko'; $timeout= 240; $dir = dirname(__FILE__); $_SERVER["REMOTE_ADDR"] = $_SERVER["REMOTE_ADDR"] ?? '127.0.0.1'; $cookie_file = $dir . '/cookies/' . md5($_SERVER['REMOTE_ADDR']) . '.txt'; $ch = curl_init($url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_FAILONERROR, true); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true ); curl_setopt($ch, CURLOPT_ENCODING, "" ); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt($ch, CURLOPT_AUTOREFERER, true ); curl_setopt($ch, CURLOPT_MAXREDIRS, 10 ); curl_setopt($ch, CURLOPT_USERAGENT, $useragent); curl_setopt($ch, CURLOPT_REFERER, 'http://www.google.com/'); if($file!=null){ if (!curl_setopt($ch, CURLOPT_FILE, $file)){ // Handle error die("curl setopt bit the dust: " . curl_error($ch)); } //curl_setopt($ch, CURLOPT_FILE, $file); $timeout= 3600; } curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout ); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout ); if($fields!=null){ $postvars = http_build_query($fields); // build the urlencoded data if($method=="POST"){ // set the url, number of POST vars, POST data curl_setopt($ch, CURLOPT_POST, count($fields)); curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars); } if($method=="GET"){ curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); $url = $url.'?'.$postvars; } } curl_setopt($ch, CURLOPT_URL, $url); $content = curl_exec($ch); if (!$content){ $error = curl_error($ch); $info = curl_getinfo($ch); die("cURL request failed, error = {$error}; info = " . print_r($info, true)); } if(curl_errno($ch)){ echo 'error:' . curl_error($ch); } else { return $content; } curl_close($ch); }
am using this simple one
´´´´ class Connect {
public $url; public $path; public $username; public $password; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_USERPWD, "$this->username:$this->password"); //PROPFIND request that lists all requested properties. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PROPFIND"); $response = curl_exec($ch); curl_close($ch);