1

I'm trying to get the content of a stream over HTTPS, but I have to go over an HTTP proxy. I'd like not to use cURL but rather use fopen with a context argument.

The thing is, I can't make it work over HTTPS (HTTP is working fine though).

This DOES NOT work :

$stream = stream_context_create(Array("http" => Array("method"  => "GET",
                                                      "timeout" => 20,
                                                      "proxy"   => "tcp://my-proxy:3128",
                                                      'request_fulluri' => True 
                                )));
echo file_get_contents('https://my-stream', false, $context); 

This DOES work (cURL) :

$url = 'https://my-stream';
$proxy = 'my-proxy:3128';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$curl_scraped_page = curl_exec($ch);
curl_close($ch);

echo $curl_scraped_page;

Does somebody know what is wrong with the first piece of code ? if it works with cURL there has to be a way to make it work with a context. I tried to change the context options to a bunch of different values woth no luck.

Any help would be greatly appreciated !

Thanks.

3
  • From what I recall, the key in the array passed to stream_context_create is the protocol that will be used. Try switching the key from http to https. Commented Dec 5, 2012 at 18:56
  • Thanks, but I tried, with no luck. I read some code snippets where people make https request without changing the key to 'https'. The only difference here is that i'm using a proxy :/ Commented Dec 5, 2012 at 19:10
  • 1
    Changing the key to https is incorrect! https:// calls will use the http context and then the underlying ssl transport context. Commented Oct 14, 2013 at 14:12

1 Answer 1

7

You did not specifiy the exact error message, try adding ignore_errors => true. But if you are getting a 400 Bad Request from Apache, the problem you are probably hitting, is a Server Name Indication & host header mismatch. There is also a PHP bug related to this: https://bugs.php.net/bug.php?id=63519

Try the following fix until this bug is resolved:

$stream = stream_context_create(array(
    'http' => array(
        'timeout' => 20,
        'proxy' => 'tcp://my-proxy:3128',
        'request_fulluri' => true 
    ),
    'ssl' => array(
        'SNI_enabled' => false // Disable SNI for https over http proxies
    )
));
echo file_get_contents('https://my-stream', false, $context);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.