标签: file-get-contents

在 PHP 中从远程服务器检索文件时处理延迟

我正在使用 PHP 来访问远程服务器上的文件和照片。我主要使用 file_get_contents() 和 copy() 函数。

有时访问一个小文本文件或照片几乎是即时的,但有时它似乎会在同一个文件上“卡住”一分钟。有时它实际上会导致我的脚本挂起,即使我停止脚本,Apache 仍会锁定几分钟。

我非常愿意接受互联网连接可能不稳定的事实。我担心的是我可以正常恢复并且不会使 Apache 崩溃 - PHP set_time_limit() 函数仅返回致命错误。另外,PHP手册中有一条注释,流操作所花费的时间不会影响脚本的运行时间。

如何从此类连接问题中恢复并允许我的脚本继续运行?为什么这会导致 Apache 挂起?

谢谢,布莱恩

php apache file-get-contents remote-server

5
推荐指数
1
解决办法
2129
查看次数

CURL或file_get_contents更新供稿列表?

我正在运行一个提要阅读器网站,那里会有很多RSS.我将尽可能经常同步这些提要,所以我找到了这两种方法.

1方法:使用CURL

$weblog_name = 'MyBlog';
$weblog_url = 'http://feeds.feedburner.com/myblog';
$ping_url = 'http://ping.feedburner.com';

$request = <<<EOT
<?xml version="1.0" encoding="iso-8859-1"?>
<methodCall>
<methodName>weblogUpdates.ping</methodName>
<params>
 <param>
  <value>
   <string>$weblog_name</string>
  </value>
 </param>
 <param>
  <value>
   <string>$weblog_url</string>
  </value>
 </param>
</params>
</methodCall>
EOT;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $ping_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_POST, true );
curl_setopt($ch, CURLOPT_POSTFIELDS, trim($request)); 
$result = curl_exec($ch);
curl_close($ch); 
Run Code Online (Sandbox Code Playgroud)

第二种方法:file_get_contents

file_get_contents("http://feedburner.google.com/fb/a/pingSubmit?bloglink=http://feeds.feedburner.com/myblog");
Run Code Online (Sandbox Code Playgroud)

我的问题是哪个是更好,更快的解决方案,一次ping至少50个Feed?

php rss curl file-get-contents

5
推荐指数
1
解决办法
1929
查看次数

file_get_contents在读取文件时显示意外输出

我想输出内联jpg图像作为base64编码的字符串,但是当我这样做时:

$contents = file_get_contents($filename);
print "<img src=\"data:image/jpg;base64,".$contents."\"/>";
Run Code Online (Sandbox Code Playgroud)

$filename带有base64图像的本地文本文件在哪里.输出如下:

<img src="data:image/jpg;base64,/9j/4A..... (the rest of the file)...." />
Run Code Online (Sandbox Code Playgroud)

而且好像没有渲染图像,但是它来自哪里?它不在文本文件中.如果删除,图像将正确显示.

php base64 image file-get-contents

5
推荐指数
1
解决办法
1397
查看次数

使用 ip 或计算机名在网络上使用 php 读取文件

当我通过 ip 或计算机名在 LAN 网络上使用 php 读取文件时。

$url = "\\\\192.168.0.200\\testshare\\1.txt";
        if(file_exists($url) == TRUE) {
            echo "OK";
        } else {
            echo "Error";
        }
Run Code Online (Sandbox Code Playgroud)

结果:总是“错误”。如果我使用函数 file_get_contents ($url , true); 结果:

Warning: file_get_contents(\\192.168.0.200\testshare\1.txt) [function.file-get-contents]: failed to open stream: Permission denied in D:\services\htdocs\test.php on line 3
Run Code Online (Sandbox Code Playgroud)

当然,文件夹“testshare”设置共享和权限每个人都完全控制。如果我用磁带浏览器:\192.168.0.200\testshare\1.txt 它读取文件 1.txt 的内容。你能帮助我吗?每个想法都帮助我,我非常感谢。

php file-get-contents file-exists

5
推荐指数
1
解决办法
1万
查看次数

php file_get_contents - javascript 执行后

基本上,我正在尝试使用 php 抓取网页,但我想在页面上的初始 javascript 执行之后执行此操作 - 我想在初始 ajax 请求之后访问 DOM 等...有什么方法可以做到这一点吗?

javascript php file-get-contents

5
推荐指数
1
解决办法
1万
查看次数

当使用浏览器打开 URL 并且 URL 有效时,file_get_contents 返回 404

我收到以下错误:

警告: file_get_contents( https://www.readability.com/api/content/v1/parser?url=http://www.redmondpie.com/ps1-and-ps2-games-will-be-playable-on- playstation-4-very-soon/?utm_source=dlvr.it&utm_medium=twitter&token=MYAPIKEY) [function.file-get-contents]:无法打开流:HTTP 请求失败!HTTP/1.1 404 NOT FOUND in /home/DIR/htdocs/readability.php 第 23 行

通过一些 Echoes,我得到了函数解析的 URL,它很好且有效,我从浏览器发出请求,一切正常。

问题是我用 file_get_contents 得到了上述错误,我真的不明白为什么。

URL 有效并且该功能未被免费托管服务阻止(所以我不需要 Curl)。

如果有人能发现我的代码中的错误,我将不胜感激!谢谢...

这是我的代码:

<?php

class jsonRes{
    public $url;
    public $author;
    public $url;
    public $image;
    public $excerpt;
}

function getReadable($url){
 $api_key='MYAPIKEY';
 if(isset($url) && !empty($url)){

    // I tried changing to http, no 'www' etc... -THE URL IS VALID/The browser opens it normally-

    $requesturl='https://www.readability.com/api/content/v1/parser?url=' . urlencode($url) . '&token=' . $api_key;
    $response = file_get_contents($requesturl);   // * here the …
Run Code Online (Sandbox Code Playgroud)

php url json file-get-contents http-status-code-404

5
推荐指数
1
解决办法
7254
查看次数

通过 file_get_contents 传递 PHP xdebug 上下文

我用来file_get_contents()查询 PHP 应用程序的另一部分。我很乐意构建一个“POST”甚至多部分查询来模拟文件上传,但我似乎无法弄清楚如何将挂钩的处理file_get_contents()放入我现有的 xdebug 会话中(正在工作,因为我可以逐步完成查询处理)。

我尝试过传递XDEBUG_SESSION当前正在处理的请求中的 cookie,但它似乎没有达到我想要的效果。

php xdebug file-get-contents

5
推荐指数
0
解决办法
344
查看次数

通过 div id 获取 file_get_contents

我试图从外部网页(https://uconn.givecorps.com/projects/3120-uconn-marching-band-tower-project)获取特定div(id =“total_raised”)的内容。这是我的代码:

$url = 'https://uconn.givecorps.com/projects/3120-uconn-marching-band-tower-project';
$content = file_get_contents($url);
$first_step = explode( '<div id="total_raised">' , $content );
$second_step = explode("</div>" , $first_step[1] );

echo $second_step[0];
Run Code Online (Sandbox Code Playgroud)

当我回显 $content 时,一切都显示正常。但当我试图仅回应筹集的美元金额时,什么也没有显示。

php file-get-contents

5
推荐指数
1
解决办法
9668
查看次数

file_get_contents 通过 php 失败,通过浏览器工作

我想要实现的目标:
\n向 API 端点发出请求,检索 XML 并随后解析结果。
\n我正在发送一个file_get_contents请求来实现此目的。

\n\n

问题:

\n\n
`file_get_Contents` fails, error:  \n\nWarning: file_get_contents(https://api.twitter.com/1.1/statuses/mentions_timeline.json):\nfailed to open stream: \n        A connection attempt failed because the connected party did not properly \nrespond after a period of time, or established connection failed because \nconnected host has failed to respond. \n
Run Code Online (Sandbox Code Playgroud)\n\n

更新 17/08

\n\n

为了巩固我目前的理解:
\n 1. PHP 失败:
\n1.a 它通过 php (超时)失败
\n1.b 它通过命令行失败(curl -G http://api.eve-central.com/api /quicklook?typeid=34 )
\n1.c file_get_contents
\n1.d file_get_contents w/ create_stream_context

\n\n

2. 有效方法:
\n2.a 通过 …

php curl timeout simplexml file-get-contents

5
推荐指数
1
解决办法
3432
查看次数

在 Powershell 中,处理超过 1GB 的文件时收到“OutOfMemoryException”

我在加载到数据仓库之前进行了一些文件清理,并遇到了文件大小调整问题:

(Get-Content -path C:\Workspace\workfile\myfile.txt -Raw) -replace '\\"', '"' | Set-Content C:\Workspace\workfile\myfileCLEAN.txt
Run Code Online (Sandbox Code Playgroud)

我的文件大约有2GB。我收到以下错误,但不知道如何纠正。

Get-Content:抛出“System.OutOfMemoryException”类型的异常,........

我不是编码员,但我确实喜欢学习,所以正在构建自己的数据仓库。因此,如果您确实做出回应,请记住我的经验水平:)

powershell file-get-contents

5
推荐指数
1
解决办法
1917
查看次数