下面的代码使用两种方式来调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)
- {
- 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);
|