下面的代碼使用兩種方式來調facebook的接口,第一種縣判斷用戶的環境是否開啟了curl庫,開啟了這個庫,就采用這種方式來獲取請求,里面詳細的參數講解大家可以參考手冊,PHP實例代碼如下:
- if(function_exists('curl_init'))
- {
- $ch = curl_init();
- curl_setopt($ch, curlopt_url, $url_with_get);
- curl_setopt($ch, curlopt_post, 1);
- curl_setopt($ch, curlopt_postfields, $post);
- curl_setopt($ch, curlopt_returntransfer, true);
- $result = curl_exec($ch);
- curl_close($ch);
- }
- else
- {
- $content = http_build_query($post)
- $content_length = strlen($content);
- $context =
- array('http' =>
- array('method' => 'post',
- 'user_agent' => $user_agent,
- 'header' => 'content-type: ' . $content_type . " " .
- 'content-length: ' . $content_length,
- 'content' => $content));
- $context_id = stream_context_create($context);
- $sock = fopen($url_with_get, 'r', false, $context_id);
- $result = '';
- if ($sock) //開源代碼Vevb.com
- {
- while (!feof($sock))
- $result .= fgets($sock, 4096);
- fclose($sock);
- }
- return $result;
- }
- }
測試代碼如下:
- $url_with_get= "http://api.facebook.com/restserver.php?method=facebook.friends.get&session_key=&api_key=1232121311&v=1.0";
- $post = array('sig'=>12312123234353);
新聞熱點
疑難解答