file_get_contents giving failed to open stream: Connection timed out error for file on same server
<p>I am trying to have a file on my server send post data as if coming from a form ("calling_file.php") to another file on the same server ("processing_file.php"). Here is the code:</p>

<pre><code>$url = 'https://www.example.com/path/to/processing_file.php';
$method = 'POST';

$eol = "\r\n";

// Create a stream
$opts = array(
    'http' =&gt; array(
        'timeout' =&gt; 30,
        'method' =&gt; $method,
        'header' =&gt; 'Content-Type: application/x-www-form-urlencoded' . $eol,
        'content' =&gt; http_build_query($xmit)
    )
);

$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$result = file_get_contents($url, false, $context); //filename, use_include_path, context

//echo($result);
$result_decode = json_decode($result,true);
</code></pre>

<p>$xmit looks something like this:</p>

<pre><code>array(
    'dx' =&gt; json_encode('[{"business_date":"2015-09-29","check_avg":10.749122993789,"guests":"422"},{"business_date":"2015-09-30","check_avg":36.735215894737,"guests":"461"},{"business_date":"2015-10-01","check_avg":12.2527145,"guests":"288"},{"business_date":"2015-10-02","check_avg":18.405998957055,"guests":"263"},{"business_date":"2015-10-03","check_avg":20.385927936508,"guests":"289"}]'),
    'period' =&gt; 5,
    'phase_code' =&gt; 3,
    'site_id' =&gt; 155
)
</code></pre>

<p>When I echo out $opts, everything looks right. http_build_query($xmit) is creating what looks to be valid post data. I have this set in processing_file.php and calling_file.php:</p>

<pre><code>ini_set('allow_url_include', '1');
ini_set('allow_url_fopen', '1');
</code></pre>

<p>When I paste: 
    <a href="http://www.example.com/path/to/processing_file.php" rel="nofollow noreferrer">http://www.example.com/path/to/processing_file.php</a></p>

<p>directly in my browser, the file opens with 200 status, all good.</p>

<p>I tried setting the owner of processing_file to be the apache user, and set chmod to 775 on it.</p>

<p>However, when calling_file.php tries to call processing_file.php with the above code, I get this:</p>

<pre><code>Warning: file_get_contents(https://www.example.com/path/to/processing_file.php): failed to open stream: Connection timed out in /var/www/example.com/functions/path/to/other/file/calling_file.php on line 219 
</code></pre>

<p>line 219 is:</p>

<pre><code>$result = file_get_contents($url, false, $context); //filename, use_include_path, context
</code></pre>

<p>I can't figure out why it can't see the file. </p>