PHP implements the method of imitating the socket request return page _php tips

Source: Internet
Author: User

This article illustrates how PHP implements a method that imitates the return page of a socket request. Share to everyone for your reference. The implementation methods are as follows:

Copy Code code as follows:
<?php
$url = "www.XXXX.com"; Make your own replacements.
$parse = Parse_url ($url); Resolves the URL and returns the component.
$host = $parse [' Host '];
$path = $parse [' path '];
$port = 80;
$timeout = 80;
$fp = @fsockopen ($host, $port, $errno, $errstr, $timeout); Open the Socket link
if (! $fp) {
echo $errno. "  -". $errstr; If error, return error code and error message
} else {
$out = "POST $path http/1.1\r\n"; The following is the HTTP request header information
$out. = "Host:". $host. " \ r \ n ";
$out. = "Accept: */*\r\n";
$out. = "connection:close\r\n";
$out. = "Cookie: $cookie \r\n\r\n";

@fwrite ($fp, $out); Write the request information to the link
$status = Stream_get_meta_data ($FP);
if (! $status [' timed_out ']) {
while (!feof ($fp)) {
if ($header = @fgets ($fp)) && ($header = = "\ r \ n" | | $header = = "\ n")) {
Break
}
}

$stop = false;
while (!feof ($FP) &&! $stop) {
$data = Fread ($fp, 8192); 8192 is the number of bytes that can be returned
$return. = $data;
}
}
Fclose ($FP);
Print_r ($return);
}

I hope this article will help you with your PHP program design.

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.