我想拥有一个独立的PHP类,在这里我想拥有一个通过cURL调用API并获取响应的函数。有人可以帮我吗?
谢谢。
只需使用下面的代码即可获得来自宁静的Web服务URL的响应,而我使用的是社交提及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; }