jjzjj

PHP: "file_get_contents()"从经过内容验证的 URL 返回 NULL

coder 2024-01-05 原文

我敢打赌它最终会变得很简单......但我可以用一只手来发现它。

问题:
当我尝试通过 file_get_contents() 获取特定本地 URL 的内容时,它返回为空字符串 ''(长度 = 0)。

我尝试过的:

    $url = 'http://localhost/bbq/index.php/company/get/id/2131/return/json';
    $results = file_get_contents($url);
    echo'<pre>Results:<br />',var_export($results),'</pre>';

当直接访问 http://localhost/bbq/index.php/company/get/id/2131/return/json 时,一切都很漂亮:{"id":"2131","value":"Acme Anvil Corp."

为了检查自己,我尝试使用 $url = 'http://www.google.com',它按预期工作,所以我尝试了单独的本地 uri,http://localhost/?phpinfo=1,它也能正常工作

问题:
我错过了什么???


[附加信息]
allow_url_fopen 开启

可能刚刚找到了东西
作为 CODEIGNITER 项目中的一项安全措施, Controller 脚本的第一行具有:

    if ( !defined('BASEPATH')) exit('No direct script access allowed');

将其注释掉并没有提供任何改进,但我认为它仍然值得一提。

此外,这里还有 HTTP 响应 header 的转储。也许有人能看到里面的东西。对我来说,这一切似乎都很简单,但我真的不知道我应该寻找什么。

array (size=13)
  0 => string 'HTTP/1.1 200 OK' (length=15)
  1 => string 'Date: Thu, 27 Dec 2012 19:58:47 GMT' (length=35)
  2 => string 'Server: Apache/2.4.2 (Win64) PHP/5.4.3' (length=38)
  3 => string 'X-Powered-By: PHP/5.4.3' (length=23)
  4 => string 'Set-Cookie: csrf_cookie_name=4ae46f9a7e612ae22a080a4e88bd349c; expires=Thu, 27-Dec-2012 21:58:47 GMT; path=/' (length=108)
  5 => string 'Set-Cookie: PHPSESSID=ah70p0ldl5qptcauei1t8ihud3; path=/' (length=56)
  6 => string 'Expires: Thu, 19 Nov 1981 08:52:00 GMT' (length=38)
  7 => string 'Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0' (length=77)
  8 => string 'Pragma: no-cache' (length=16)
  9 => string 'Refresh: 0;url=http://localhost/bbq/index.php/index.php/user/login' (length=66)
  10 => string 'Content-Length: 0' (length=17)
  11 => string 'Connection: close' (length=17)
  12 => string 'Content-Type: text/html' (length=23) 

[/附加信息]

顺便说一句,我正在运行:

  • Win7 64 位上的 WAMPSERVER
  • Apache 2.4.2
  • PHP 5.4.3
  • 代码点火器 2.1.2

最佳答案

现在开始调试:

# we want text output for debugging. Either this works or you need to look into
# source in your browser (or even better, debug with the `curl` commandline client.)
header('Content-Type: text/plain');

# For quick and dirty debugging - start to tell about if something is worth
# to warn or notice about. (do logging in production)
error_reporting(~0); ini_set('display_errors', 1);

$url = 'http://localhost/bbq/company/get/id/2131/return/json';
$results = file_get_contents($url);
var_dump($http_response_header, $results);

# hard exit, flush all (potential) buffers.
die();

由于 var_dump,对文件的请求现在必须至少返回一些文本输出。您还可以在 header 调用下方的开头添加一些标记输出,因此很明显脚本已被调用并开始运行。

然而,更好的变体是使用单步调试器并创建带断点的调试 session 。我推荐 Xdebug。

关于PHP: "file_get_contents()"从经过内容验证的 URL 返回 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14059923/

有关PHP: "file_get_contents()"从经过内容验证的 URL 返回 NULL的更多相关文章

  1. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  2. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  3. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  4. ruby - 将数组的内容转换为 int - 2

    我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]

  5. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  6. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  7. ruby-on-rails - rails : save file from URL and save it to Amazon S3 - 2

    从给定URL下载文件并立即将其上传到AmazonS3的更直接的方法是什么(+将有关文件的一些信息保存到数据库中,例如名称、大小等)?现在,我既不使用Paperclip,也不使用Carrierwave。谢谢 最佳答案 简单明了:require'open-uri'require's3'amazon=S3::Service.new(access_key_id:'KEY',secret_access_key:'KEY')bucket=amazon.buckets.find('image_storage')url='http://www.ex

  8. ruby - 如何使用 Ruby aws/s3 Gem 生成安全 URL 以从 s3 下载文件 - 2

    我正在编写一个小脚本来定位aws存储桶中的特定文件,并创建一个临时验证的url以发送给同事。(理想情况下,这将创建类似于在控制台上右键单击存储桶中的文件并复制链接地址的结果)。我研究过回形针,它似乎不符合这个标准,但我可能只是不知道它的全部功能。我尝试了以下方法:defauthenticated_url(file_name,bucket)AWS::S3::S3Object.url_for(file_name,bucket,:secure=>true,:expires=>20*60)end产生这种类型的结果:...-1.amazonaws.com/file_path/file.zip.A

  9. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  10. ruby-on-rails - 如何在我的 Rails 应用程序 View 中打印 ruby​​ 变量的内容? - 2

    我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby​​中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R

随机推荐