What is the PHP fsockopen function return value

Source: Internet
Author: User
Tags ereg
Ask PHP Fsockopen function return value
Ask the following code. How to tell if the Fsockopen connection is successful?

Because if it fails, fwrite will generate a lot of error messages in my space.
PHP warning:fwrite (): supplied argument is not a valid stream resource in/home2/

I want to add judgment, if Fsockopen fail to skip, avoid error execution fwrite

Thank you! Please help

$fp = Fsockopen ("www.example.com", $errno, $errstr, 30);
if (! $fp) {
echo "$errstr ($errno) \ n ";
} else {
$out = "get/http/1.1\r\n";
$out. = "host:www.example.com\r\n";
$out. = "connection:close\r\n\r\n";


Fwrite ($fp, $out);
while (!feof ($fp)) {
Echo fgets ($FP, 128);
}
Fclose ($FP);
}


------Solution--------------------
The code is no problem, this is the PHP official website example. You should stick your code out.

Alternatively, you can try this, if (Get_resource_type ($fp) = = ' stream ') fwrite ($fp, $out);
------Solution--------------------
You are not judging here. You can just add a withdrawal.

if (! $fp) {
echo "$errstr ($errno)
\ n ";
Exit
} else {
....
------Solution--------------------
while (!ereg (' $ OK ', $banreturn) && (!ereg (' 302 Found ', $banreturn)) && ($attemp <2)) {
$connection = Fsockopen ($hostname, $port, $errno, $errstr, $timeout);
Stream_set_timeout ($connection, 5);
Fwrite ($connection, $headers); The error record is generated here, but not every time.

Why not first check if Open is successful or write?
  • 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.