N
N
nonbody2018-11-21 00:25:45
PHP
nonbody, 2018-11-21 00:25:45

Why won't PHP connect over ssl via fsockopen?

$f = get('https://twitter.com/', 'https://twitter.com/', $_SERVER['HTTP_USER_AGENT'], '', true);
var_dump($f);
function get($url, $ref, $ua, $cook, $ssl)
{
    $url = parse_url($url);
    $line="";
    $connecthost = $ssl ? 'ssl://' . $url['host'] : $url['host'];
    $port = $ssl ? 443  : 80;
    $fp=fsockopen($connecthost, $port, $errno, $errstr, 60);
    
        if(!$fp) {
        return $connecthost . ':    ' . $errstr;
        }
            else
            {
                $query = isset($url['query']) ? '?' . $url['query'] : '';
                $path = $url['path'] . $query;
                $headers = "GET $path HTTP/1.1\r\n";
                $headers .="Host: {$url['host']}\r\n";
                $headers .="User-Agent: $ua\r\n";
                $headers .= "Accept-Language: en,en-US;q=0.9,en;q=0.8\r\n";
                $headers .="Cookie: $cook\r\n";
                $headers .="Connection: Close\r\n";
                $headers .="Referer: $ref\r\n\r\n";
                
                fwrite($fp, $headers);
                
                
                    while(!feof($fp)){
                            
                        $line .= fgets($fp, 1024);
                    }
                
                
                fclose($fp);
            }

    return $line;
}

var_dump($f) returns nothing. The error_log is empty.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton [RuleZzz] Shipulin, 2018-11-21
@rul22rus

Perhaps the problem is that the allow_url_fopen parameter in php.ini is disabled.
It may also be that the SSL module is not enabled, look in phpinfo()

A
Arris, 2018-11-22
@Arris

$fp=fsockopen($connecthost, $port, $errno, $errstr, 60);

php.net/manual/en/function.fsockopen.php
If this parameter is provided, then in case of an error in the connect() system call, it will take the number of this error.
If the value of the errno parameter is 0 and the function returns FALSE, then the error occurred before the connect() call. In most cases, this indicates problems with socket initialization.
Read? Is it meaningful? Or is the example stupidly copied from stackoverflow?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question