PHP network function fsockopen how to implement Socket link _ PHP Tutorial

Source: Internet
Author: User
Tags network function
PHP network function fsockopen how to implement Socket link. Syntax: intfsockopen (stringhostname, intport, int [errno], string [errstr], int [timeout]); return value: integer function type: Network System Description: Current PHP network functionSyntax: int fsockopen (string hostname, int port, int [errno], string [errstr], int [timeout]);

Return value: integer

Function type: Network System Description: Currently, the PHP network function fsockopen provides two Socket data stream interfaces: AF_INET for Internet and AF_UNIX for Unix. When used on the Internet, the hostname and port parameters represent the URL and port numbers respectively. In UNIX, you can use IPC. the hostname parameter indicates the socket path and port is set to 0. The time out option that can be omitted indicates how long the connection is interrupted. After using this function, a file pointer is returned for the file function, including fgets (), fgetss (), fputs (), fclose (), and feof (). The errno and errstr parameters can also be omitted and used for error handling. Using this function, the block mode is used for processing. you can use set_socket_blocking () to convert it to the no-hold mode.

Example of using the PHP network function fsockopen. This example is used to simulate an HTTP connection.

 
 
  1. $fp = fsockopen("php.wilson.gs", 80, &$errno, &$errstr, 10);
  2. if(!$fp) {
  3. echo "$errstr ($errno)
    n";
  4. } else {
  5. fputs($fp,"GET / HTTP/1.0nHost: php.wilson.gsnn");
  6. while(!feof($fp)) {
  7. echo fgets($fp,128);
  8. }
  9. fclose($fp);
  10. }
  11. ?>

Through the above example of using the PHP network function fsockopen, do readers have basically mastered the usage of this function?


Optional int fsockopen (string hostname, int port, int [errno], string [errstr], int [timeout]); return value: integer function type: network system description: current PHP network functions...

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.