I’m trying to load URLs using Coral CDN but I could not make it work if I use my own context resource.
<?php
$url = 'http://www.stackoverflow.com/';
echo substr(htmlentities(file_get_contents($url)),0,100); // works OK
echo '<hr />';
$url = 'http://www.stackoverflow.com.nyud.net/'; // CORAL content distribution network
echo substr(htmlentities(file_get_contents($url)),0,100); // works OK
echo '<hr />';
$options = array(
'http'=>array(
'method'=>"POST",
'header'=>
"Host: www.stackoverflow.com.nyud.net\r\n".
"Connection: keep-alive\r\n".
"Content-Length: 3\r\n".
"Cache-Control: max-age=0\r\n".
"Origin: http://www.stackoverflow.com.nyud.net\r\n".
"User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1\r\n".
"Content-Type: application/x-www-url-form-encoded\r\n".
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n".
"Accept-Encoding: gzip,deflate,sdch\r\n".
"Accept-Language: en-US,en;q=0.8\r\n".
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3",
'content'=>'a=1'
));
$context = stream_context_create($options);
echo file_get_contents($url,false,$context); // 405 error?
?>
This is the actual error I get:
Warning: file_get_contents(http://www.stackoverflow.com.nyud.net/) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 405 in C:\…\lab\php-exec\index.php(4) : eval()’d code on line 29
I know curl might work but I insist to use file_get_contents(), how do you think can I solve this problem?
Thanks!
Well, I think you just can’t send a POST request to
*.nyud.netwebsite, see a basic post.Request:
Response:
Which seems to be normal because it acts as a CDN for any website, no mater if the website is ok or not.
Since a POST request means you want to submit something to the website, to create, update, or what ever, CoralCDN can’t handle it because it can’t submit something to an other website on its behalf. It could be a security issue. Nobody can POST what ever they want to a website anonymously…
If you want to send a parameter to the request, put them in the url (GET request are OK) but there won’t be any solution to send a POST request to CoralCDN, even using cURL.