php - 使用自己的上下文资源的 Coral CDN 的 file_get_contents 将不起作用

标签 php http proxy cdn

我正在尝试使用 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?
?>

这是我得到的实际错误:

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

我知道curl可能有用,但我坚持使用 file_get_contents() ,你认为我该如何解决这个问题?

谢谢!

最佳答案

好吧,我想你只是不能*.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,无论该网站是否正常。

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

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

关于php - 使用自己的上下文资源的 Coral CDN 的 file_get_contents 将不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12518731/

相关文章:

php - Symfony 2 中 Zend\Validate\Db\NoRecordExists 的模拟

php - 禁止访问!尝试运行 php 文件时出现 403 错误

wcf - net.tcp 和 http 有什么区别?

http - JMeter可以支持发送HTTP1.1的管道请求吗?

http - flutter http header

firefox - 如何使Firefox使用代理服务器进行本地主机连接

apache - ProxyHTMLURLMap 没有以正确的方式重写 URL

php - Divi Builder 自定义模块

php - 通过 PDO 和 mysql 从可能不安全的输入创建新数据库

node.js - nodejs 表示 : hostname with port from the req object