PHP simulates post/get operations and simple implementation of open community Interfaces

Source: Internet
Author: User

I attended a PHP interview last year. I asked a question to use the PHP socket function to simulate post. At that time, I was vague about this, so I didn't answer it. Recently, social game (community game) is being developed. It is found that there are many places to use post requests. Therefore, we can sum up and summarize it for you to facilitate query in future use.

 

To facilitate third-party applications to use Facebook's functions, Facebook provides a series of interfaces, such as obtaining personal user information and friend information, send feed (dynamic information) to your own space, invite friends and some community plug-ins. In general, Facebook has encapsulated the interfaces It provides. When we use them, we only need to download the client library and call the functions in it to obtain the corresponding functions. If it does not provide the client class library, we need to encapsulate it by ourselves, and we need to access the Facebook interface file to obtain the corresponding data. Here is an operation to obtain friend information.

Facebook interface address: http://api.facebook.com/restserver.php

Therefore, we need to send a post/GET request through the program and obtain the result. We will not talk about facebok's work on request verification for the moment. We will focus on how requests are implemented through requests.

The simple 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 );

First, check whether the curl library is installed in the environment. If yes, use the curl function to implement the request:
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
{// Access using a file stream
$ 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;
}

}
When using curl, set the value of curlopt_returntransfer to save the returned results to the variable.
One of the following methods is to convert access to a normal file stream. In addition to the stream_context_create function, we can also use the fsocketopen function for access. There are a lot of specific code.
You can find it on the network.

Next we will discuss another topic: How does a community like Facebook provide such an interface? That's how these interfaces are implemented.

For third-party developers, we do not care about the specific internal implementation, as long as you can return the correct formatted data according to the parameters we provide. Therefore, Facebook needs to verify the parameters of third-party developers.
The related content calls the corresponding interface and organizes the result into XML or JSON format.
As a community, Facebook has excellent interfaces for friend processing, and I have also developed communities to know that friends are one of the core functions of the community. There are many interfaces. You only need to encapsulate it for external use.
The task of such an interface is mainly to verify whether the request is valid, prevent the leakage of user data caused by bad requests, and build a good interface architecture to call different interfaces according to different parameters.

Restserver. php

.......
Try
{
Validate ($ Params );

Dispatch ($ method );
}
Catch (exception $ E)
{
}
Verify the validity of the call. If the call is invalid, an exception is thrown. Then, related functions are called based on the user's request interface. For example, the interface called above to obtain friend information is called, call the friend interface in Facebook.
Returns friend information.


 


 

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.