PHP uses the socket Curl file_get_contents to imitate the user post to send the information

Source: Internet
Author: User
Tags curl php tutorial socket strlen

Development often used to imitate the user post information on the program, I introduced several methods, the need for a friend to refer to.
# <?php Tutorial
# /**
# * Socket version
# * Use method:
# * $post _string = "App=socket&version=beta";
# * Request_by_socket (' facebook.cn ', '/restserver.php ', $post _string);
# */
# function Request_by_socket ($remote _server, $remote _path, $post _string, $port =, $timeout = 30) {
# $socket = Fsockopen ($remote _server, $port, $errno, $errstr, $timeout);
# if (! $socket) Die ("$errstr ($errno)");
#
# fwrite ($socket, "POST $remote _path http/1.0");
# fwrite ($socket, "User-agent:socket Example");
# fwrite ($socket, "HOST: $remote _server");
# fwrite ($socket, "content-type:application/x-www-form-urlencoded");
# fwrite ($socket, "content-length:") strlen ($post _string) +8. "");
# fwrite ($socket, "accept:*/*");
# fwrite ($socket, "");
# fwrite ($socket, "mypost= $post _string");
# fwrite ($socket, "");
#
# $header = "";
# while ($str = Trim (fgets ($socket, 4096))) {
# $header. = $str;
#     }
#
# $data = "";
# while (!feof ($socket)) {
# $data. = Fgets ($socket, 4096);
#     }
#
# return $data;
# }
#
Close socket more detailed tutorials please see
Http://www.111cn.net/phper/30/7cadb3c9195ac7d8ac9104da61a25c6e.htm

#/**  
# * Curl version   
# * Use method:  
# * $post _string = "App=request&versi On=beta ";  
# * Request_by_curl (' http://facebook.cn/restServer.php ', $post _string);  
*/  
# function Request_by_curl ($remote _server, $post _string) {  
#      $ch = Curl_init ();  
#     curl_setopt ($ch, Curlopt_url, $remote _server);   
#     curl_setopt ($ch, Curlopt_postfields, ' mypost= '. $post _string);   
#     curl_setopt ($ch, curlopt_returntransfer,true);  
#      curl_setopt ($ch, Curlopt_useragent, "Jimmy's Curl Example beta");  
#      $data = curl_exec ($ch);  
#     curl_close ($ch);  
#      return $data;  
#} 

Curl Library can be simple and effective to grasp the Web page, you only need to run a script, and then analyze the pages you crawl, and then you can get the data you want in the program. Whether you want to take part of the data from a link, or fetch an XML file and import it into a database tutorial, curl is a powerful PHP library, even if it is simply getting the Web content. This article mainly describes if you use this PHP library.

Curl Reference

Http://www.111cn.net/phper/18/curl-php.htm


# /**
# * Other Versions
# * Use method:
# * $post _string = "App=request&version=beta";
# * Request_by_other (' http://facebook.cn/restServer.php ', $post _string);
# */
# function Request_by_other ($remote _server, $post _string) {
# $context = Array (
# ' HTTP ' =>array (
# ' method ' => ' POST ',
# ' header ' => ' content-type:application/x-www-form-urlencoded '.
# ' User-agent:jimmy ' s POST Example beta '.
# ' Content-length: '. strlen ($post _string) +8,
# ' content ' => ' mypost= '. $post _string)
#         );
# $stream _context = stream_context_create ($context);
# $data = file_get_contents ($remote _server,false, $stream _context);
# return $data;
# }
#
#?>

The file_get_contents () function reads the entire file into a string.

As with file (), the difference is that file_get_contents () reads the file into a string.

The file_get_contents () function is the preferred method for reading the contents of a file into a string. Memory mapping technology is also used to enhance performance if supported by the operating system.

Reference documents
Http://www.111cn.net/phper/24/f8d52eaae81ea27383375ead36ffbd4d.htm

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.