PHP Analog post| Get operation implements code _php tips

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.