doubaoxue5788 2013-01-06 22:45
浏览 49
已采纳

没有设置主机头

I have a PHP script that primes my website cache.

I'm trying to bypass the Nginx frontend and grab the headers from the Apache backend.

The following example works (via command line) :

curl -I -H "Host: example.com" 127.0.0.1

However, when I try to do the same in PHP - it does not.

$headers = array("Host: example.com");
$url = "127.0.0.1";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt ($ch, CURLOPT_HEADER, true);
curl_setopt ($ch, CURLOPT_NOBODY, true);
curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers);
$ret = curl_exec ($ch);
curl_close ($ch);

echo "$ret";

This always returns the first website listed in my apache virtualhost files - not the website listed in the 'host' http headers.

Any idea why it works just fine via command line - but not in the php script?

  • 写回答

2条回答 默认 最新

  • drbe16008 2013-01-06 22:50
    关注

    I don't know if it is possible at all, however what I would try is to pass the URL at init:

    $ch = curl_init($url);
    

    And then not with curl_setopt. It's just a suggestion to test, no guarantee this helps. So more a lengthy comment.

    You can also enable verbose mode and see what specifically curl sends for request headers, I have outlined this in an answer here: Bad Request. Connecting to sites via curl on host and system. This should show what is going on behind the curtain, very useful if you do not yet sniff the network traffic.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度