Recently developed social game, found that the use of this thing is still relatively ordinary, here to make a summary, to save some memory for their own, and also hope to help everyone.
First look at the requirements, if we develop social game on Facebook, we need to call its interface to get the user's friend information on Facebook. At this point we are going to visit an address provided by Facebook, and of course when you visit him, he needs to verify your visit and prevent illegal requests. This time you have to post|get some parameters to it.
such as the following address:
Copy Code code as follows:
$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);
How to get data from this address, simply introduce the following code:
Copy Code code as follows:
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. "\ r \ n".
' 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;
}
}
The code above uses two ways to tune Facebook's interface, and the first county determines whether the user's environment is open to the Curl library and opens the library, using this approach to get the request. Inside detailed parameters to explain you can refer to the manual.
Here is a hint, because we usually need to get the return result of the calling interface, so we want to set the value of Curlopt_returntransfer and return the result to the variable.
The second approach is intuitive and translates the URL request into a file stream to process.