gpt4 book ai didi

php - 不能使用动态创建位置的 file_get_contents

转载 作者:行者123 更新时间:2023-12-04 06:59:09 26 4
gpt4 key购买 nike

我正在尝试使用 file_get_contents() 从页面获取 html。
以下效果很好:file_get_contents('http://www.mypage.com?Title=Title')但以下会导致错误:

$Title = 'Title';
file_get_contents("http://www.mypage.com?Title=$Title")

错误是:
Bad Request

Your browser sent a request that this server could not understand.
The request line contained invalid characters following the protocol string.

Apache/1.3.41 Server at eiit.org Port 80

有谁知道为什么?

最佳答案

您正在使用带单引号的字符串;并且没有带单引号的变量插值。

这意味着您尝试获取的 URL 是 http://www.mypage.com?Title=$Title ,而不是 http://www.mypage.com?Title=Title .

您应该使用双引号字符串来进行变量插值:

$Title = 'Title';
file_get_contents("http://www.mypage.com?Title=$Title");

如果这仍然不起作用:
  • 检查您的 URL 是否正常:而不是直接将其传递给 file_get_contents ,将它存储在一个变量中,并回显它——只是为了确保它是正确的。
  • 为什么您的 URL 中没有页面名称?
  • 您有域名:www.mypage.com
  • 还有一个参数+值:Title=Title
  • 但是没有文件/页面?即,为什么你没有像 http://www.mypage.com/index.php?Title=$Title 这样的东西?甚至 http://www.mypage.com/?Title=$Title ?
  • 您可能需要urlencode您在 URL 中作为参数传递的值。
  • 关于php - 不能使用动态创建位置的 file_get_contents,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2152544/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com