PHP simulates POST|GET operation implementation code_PHP tutorial

WBOY
Release: 2016-07-21 15:35:24
Original
745 people have browsed it

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 of all, let’s take a look at the requirements. If we develop Facebook social game, you 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 following address:

Copy code The code is 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, briefly introduce the following code:
Copy 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);
$context =
array('http' =>
array('method' => 'POST',
'user_agent' => $user_agent,
'header' => 'Content-Type: ' . $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 method is to determine whether the curl library is enabled in the user's environment. If the library is enabled, this method is used to obtain the request. You can refer to the manual for detailed parameter explanations.
A reminder here is that 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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322232.htmlTechArticleI recently developed a social game and found that using this thing is still relatively ordinary. Here is a summary to save some for myself. Memory, and I hope it will be helpful to everyone. First let’s take a look at the requirements, if...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!