1

我正在尝试使用Coral CDN加载 URL,但如果我使用自己的上下文资源,我无法使其工作。

<?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(),您认为我该如何解决这个问题?

谢谢!

4

2 回答 2

1

好吧,我认为您无法向网站发送 POST 请求*.nyud.net,请参阅基本帖子

要求:

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,无论该网站是否正常。

由于 POST 请求意味着您想向网站提交内容、创建、更新或其他任何内容,CoralCDN 无法处理它,因为它无法代表它向其他网站提交内容。这可能是一个安全问题。没有人可以匿名将他们想要的内容发布到网站...

如果您想向请求发送参数,请将它们放在 url 中(GET 请求是可以的),但是即使使用 cURL,也没有任何解决方案可以向 CoralCDN 发送 POST 请求。

于 2012-10-04T22:03:58.953 回答
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
于 2012-09-28T09:19:06.270 回答