I recently developed a social game and found that using this thing is still relatively mundane. I will make a summary here to save some memories for myself and hope it will be helpful to everyone.
First, let’s take a look at the requirements. If we develop a social game on Facebook, we need to call Its interface to obtain the user's friend information on Facebook. At this time, we have to access an address provided by Facebook. Of course, when you access it, it needs to verify your access to prevent illegal requests. At this time, you have to post|get some parameters to it.
Such as the address below:
Copy the code The code is as follows:
$url_with_get= "http://api.facebook.com/restserver.php?method=facebook.friends.get&sessi
$post = array ('sig'=>12312123234353);
Copy the code The code is 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)
. $content_type . "rn" .
'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 above code uses two methods to adjust the facebook interface. The first one is to determine whether the curl library is enabled in the user's environment and enable this library. , use this method to obtain the request. You can refer to the manual for detailed parameter explanations.
A reminder here, since we usually need to get the return result of the calling interface, we need to set the value of CURLOPT_RETURNTRANSFER and return the result to the variable.
The second method is intuitive, converting url requests into file streams for processing.