2

I am using this code to check a proxy server, before doing anything else. When the connection is ok, I have a file_get_contents line to connect to a website.

$host = '123.45.678.90'; 
$port = 80; 
$waitTimeoutInSeconds = 1; 
if($fp = fsockopen($host,$port,$errCode,$errStr,$waitTimeoutInSeconds)){   
    $aContext = array(
    'http' => array(
    'proxy' => 'tcp://123.45.678.90:80',
    'request_fulluri' => true,
    ),
    );
    $cxContext = stream_context_create($aContext);

    // connect to website using a proxy server
    $file_content = file_get_contents('https://www.anything.com', False, $cxContext);
} else {
   // It didn't work 
} 
fclose($fp);

But though the proxy connection was succesfull, I see this: Warning Cannot connect to HTTPS server through proxy? Is there any chance to have an if/else statement, that allows me to do something if there is no warning and stop doing something if there is a warning? I have looked and googled a lot, but haven't found anything I could try.

Thanks for your help!

Robin Alexander
  • 984
  • 2
  • 13
  • 29

2 Answers2

0

try to use curl instead of file_get_contents and it will work

<?php

$url = 'https://www.google.com';
// to check your proxy
// $url = 'http://whatismyipaddress.com/';
$proxy = '50.115.194.97:8080';

// create curl resource
$ch = curl_init();

// set options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // read more about HTTPS     http://stackoverflow.com/questions/31162706/how-to-scrape-a-ssl-or-https-    url/31164409#31164409
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1;     en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');

// $output contains the output string
$output = curl_exec($ch);

// close curl resource to free up system resources
curl_close($ch); 

echo $output;

?>

from PHPhil

Bara' ayyash
  • 1,913
  • 1
  • 15
  • 25
  • Your solution probably works, but it doesn't address the OP's problem, or has any value to future users with the same problem. – Pedro Lobito May 24 '17 at 09:08
  • file_get_contents() is a simple screwdriver. Great for simple GET requests where the header, HTTP request method, timeout, cookiejar, redirects, and other important things do not matter. cURL with setopt is a powerdrills with almost every option you can think of. – Bara' ayyash May 24 '17 at 09:10
  • Puuuh, I am really very new to cURL. I don not know how to use it or how to get an if/else in there. – Robin Alexander May 24 '17 at 09:14
  • just put the if and else for this line `curl_setopt($ch, CURLOPT_PROXY, $proxy); ` – Bara' ayyash May 24 '17 at 09:16
  • @vloryan Your problem is related to an outdated version of `OpenSSL` and it doesn't matter if you use `cUrl` or `file_get_contents()`, it won't work unless you set `CURLOPT_SSL_VERIFYPEER` to `false`, which is **unsafe** – Pedro Lobito May 24 '17 at 09:17
  • Hm, ok. I think I can't update the `OpenSSL` version on my shared hosting. Damn! Is there a workaround for _if there is a warning, do this, else do that_? – Robin Alexander May 24 '17 at 09:19
0

if($fp = fsockopen($host,$port,$errCode,$errStr,$waitTimeoutInSeconds) doesn't make sense, if this proxy is working, you can simply use:

$aContext = array(
    'http' => array(
        'proxy' => 'tcp://123.45.678.90:80',
        'request_fulluri' => true,
    ),
);
$cxContext = stream_context_create($aContext);
$file_content = file_get_contents('https://google.com', False, $cxContext);
print $file_content;
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268