我正在尝试使用Coral CDN加载网址,但如果我使用自己的上下文资源,则无法使其正常工作。
<?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?
?>
这是我得到的实际错误:
警告:file_get_contents(http://www.stackoverflow.com.nyud.net/)[function.file-get-contents]:无法打开流:HTTP请求失败! C:\ ... \ lab \ php-exec \ index.php(4)中的HTTP / 1.0 405: 29 上的eval()代码
我知道curl可能有效,但我坚持使用file_get_contents(),您认为我怎么能解决这个问题?
谢谢!
答案 0 :(得分:1)
好吧,我认为您无法向*.nyud.net
网站发送POST请求,请参阅basic post。
请求:
POST / HTTP/1.1
Host: www.stackoverflow.com.nyud.net
Accept: */*
Content-Length: 3
Content-Type: application/x-www-form-urlencoded
a=1
响应:
HTTP/1.0 405
date: Thu, 04 Oct 2012 21:53:32 GMT
server: CoralWebPrx/0.1.20 (See http://coralcdn.org/)
content-type: text/html
connection: close
这似乎是正常的,因为它充当任何网站的CDN,如果网站没有问题,则无需担任CDN。
由于POST请求意味着您要向网站提交内容,创建,更新或任何内容,CoralCDN无法处理它,因为它无法代表代表其他网站提交内容/ strong>即可。这可能是一个安全问题。没有人可以匿名发布他们想要的任何网站...
如果你想向参数发送一个参数,把它们放在url中(GET请求没问题)但是没有任何解决方案可以向CoralCDN发送POST请求,即使使用cURL也是如此。
答案 1 :(得分:0)
错误405表示“此请求方法不允许。”
您可以将请求方法从POST更改为GET。它可能有用。
<?php
$url = 'http://www.stackoverflow.com.nyud.net/';
$options = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n"
)
);
$context = stream_context_create($options);
echo file_get_contents($url,false,$context); //works