标签: file-get-contents

file_get_contents()的更快替代品

目前我正在使用file_get_contents()将GET数据提交到一个站点数组,但是在执行页面时我收到此错误:

致命错误:超出最长执行时间30秒

我真正希望脚本做的就是开始加载网页,然后离开.每个网页最多可能需要5分钟才能完全加载,我不需要它完全加载.

这是我目前拥有的:

        foreach($sites as $s) //Create one line to read from a wide array
        {
                file_get_contents($s['url']); // Send to the shells
        }
Run Code Online (Sandbox Code Playgroud)

编辑:为了清除任何混淆,此脚本用于在其他服务器上启动脚本,不返回任何数据.

编辑:我现在正试图使用​​cURL来做这个技巧,通过设置一秒的超时使其发送数据然后停止.这是我的代码:

        $ch = curl_init($s['url']); //load the urls
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1); //Only send the data, don't wait.
        curl_exec($ch); //Execute
        curl_close($ch); //Close it off.
Run Code Online (Sandbox Code Playgroud)

也许我把选项设置错了.在我们发言时,我正在查看一些手册.只是给你一个更新.谢谢你们所有帮助我的人.

编辑:啊,发现了问题.我使用的是CURLOPT_CONNECTTIMEOUT而不是CURLOPT_TIMEOUT.哎呦.

但是现在,脚本没有触发.它们每个都使用ignore_user_abort(TRUE); 所以我无法理解这个问题

哈,划伤那个.现在工作.非常感谢大家

php curl file-get-contents curl-multi

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

使用file_get_contents将html表解析为php数组

我试图将这里显示的表解析成一个多维的PHP数组.我使用以下代码但由于某种原因它返回一个空数组.在网上搜索后,我发现这个网站是我从中获得parseTable()函数的地方.通过阅读该网站上的评论,我发现该功能完美无缺.所以我假设我从file_get_contents()获取HTML代码的方式有问题.对我做错了什么的想法?

<?php

$data = file_get_contents('http://flow935.com/playlist/flowhis.HTM');

function parseTable($html)
{
  // Find the table
  preg_match("/<table.*?>.*?<\/[\s]*table>/s", $html, $table_html);

  // Get title for each row
  preg_match_all("/<th.*?>(.*?)<\/[\s]*th>/", $table_html[0], $matches);
  $row_headers = $matches[1];

  // Iterate each row
  preg_match_all("/<tr.*?>(.*?)<\/[\s]*tr>/s", $table_html[0], $matches);

  $table = array();

  foreach($matches[1] as $row_html)
  {
    preg_match_all("/<td.*?>(.*?)<\/[\s]*td>/", $row_html, $td_matches);
    $row = array();
    for($i=0; $i<count($td_matches[1]); $i++)
    {
      $td = strip_tags(html_entity_decode($td_matches[1][$i]));
      $row[$row_headers[$i]] = $td;
    }

    if(count($row) > 0)
      $table[] = $row;
  }
  return $table;
}

$output = parseTable($data);

print_r($output);

?>
Run Code Online (Sandbox Code Playgroud)

我希望我的输出数组看起来像这样:

1
-- …

php file-get-contents html-parsing

13
推荐指数
2
解决办法
7万
查看次数

PHP中的简单JSON请求

我有以下json

country_code({"latitude":"45.9390","longitude":"24.9811","zoom":6,"address":{"city":"-","country":"Romania","country_code":"RO","region":"-"}})
Run Code Online (Sandbox Code Playgroud)

我只想要country_code,我如何解析它?

我有这个代码

<?php
$json = "http://api.wipmania.com/jsonp?callback=jsonpCallback";
$jsonfile = file_get_contents($json);

var_dump(json_decode($jsonfile));
?>
Run Code Online (Sandbox Code Playgroud)

它返回NULL,为什么?

谢谢.

php json file-get-contents

13
推荐指数
2
解决办法
6万
查看次数

如何将SOAP调用交换到cURL,以便在allow_url_fopen限制内工作?

我最近遇到了一个问题,即对ID3Global /地址服务的SOAP调用突然停止工作(它们之前工作正常).我认为它与托管服务提供商关闭allow_url_fopen我们的服务器有关,现在意味着该服务不起作用.

我被告知我需要切换到使用cURL来获取文件(WSDL),因为file_get_contents需要在php.ini文件中设置'allow_url_fopen'才能使其工作.但是我似乎没有file_get_contents在我的文件中使用来获取WSDL文件.

如何切换到使用cURL?

这是我的PHP文件调用SOAP地址:

<?php
ini_set("soap.wsdl_cache_enabled", "0");

$username = 'xxxxxxx@xxxxxxxx.com';
$password = 'xxxxxxx';

$profile_id = 'xxxxxx-xxxx-xxxx-xxxx-xxxxxxxx';

// Live WSDL
$wsdl = 'https://id3global.com/ID3gWS/ID3global.svc?wsdl';

$postcode = $_POST['ZipPostcode'];

/**
 * Method to arrange the address into a sane
 * order for displaying back to the user
 *
 * @param $item
 * @param $key
 * @internal param $address
 */
function sortAddress(&$item, $key)
{
    // Convert the object to an array
    $address = (array) $item;

    // Reorder the …
Run Code Online (Sandbox Code Playgroud)

php curl soap soap-client file-get-contents

13
推荐指数
1
解决办法
1347
查看次数

使用file_get_contents()加载远程xml页面

我在互联网上看到了类似的一些问题,没有答案.

我想将远程XML页面的源返回到字符串中.出于此问题的目的,远程XML页面是:

http://www.test.com/foo.xml
Run Code Online (Sandbox Code Playgroud)

在常规的Web浏览器中,我可以查看页面,源代码是XML文档.file_get_contents('http://www.test.com/foo.xml')但是,当我使用它时,它返回一个带有相应URL的字符串.

是否可以检索XML组件?我不在乎它是否使用file_get_contents,只是一些可行的东西.

php xml file-get-contents

12
推荐指数
2
解决办法
3万
查看次数

使用带参数的file_get_contents从PHP获取请求

我想向外部站点发送GET请求,但也想发送一些参数

例如,我要向example.com发送获取请求

我想执行www.example.com/send.php?uid=1&pwd=2&msg=3&phone=3&provider=xyz

我的代码是:

$getdata = http_build_query(
array(
    'uid' => '1',
    'pwd' => '2',
 'msg'=>'3',
 'phone'=>'9999',
 'provider'=>'xyz'
 )
);

$opts = array('http' =>
 array(
    'method'  => 'GET',
    'content' => $getdata
)
);

$context  = stream_context_create($opts);

$result = file_get_contents('http://example.com/send.php', false, $context);
Run Code Online (Sandbox Code Playgroud)

我收到服务器错误.

php get http file-get-contents

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

file_get_contents():php_network_getaddresses:getaddrinfo失败

我正在尝试使用cron作业将一些值从一台服务器更新到另一台服务器.我使用file_get_contents().

奇怪的是偶尔会失败.一分钟它工作,另一分钟它将无法工作.我收到这两个错误:

PHP警告:file_get_contents():php_network_getaddresses:getaddrinfo失败:名称或服务未知

PHP警告:file_get_contents():无法打开流:php_network_getaddresses:getaddrinfo failed:名称或服务未知

和:

PHP警告:file_get_contents():php_network_getaddresses:getaddrinfo失败:没有与主机名关联的地址

PHP警告:file_get_contents():无法打开流:php_network_getaddresses:getaddrinfo failed:没有与主机名关联的地址

我已登录遇到此问题的服务器.使用ping或traceroute工作正常,所以我排除了DNS问题.我还添加了谷歌DNS名称服务器,以确保.使用完全相同的代码的第三个服务器没有这个问题,所以它看起来是"调用"端的问题,而不是"响应"端.在这种情况下,不能使用IP替换主机名.

有谁知道如何解决这个问题?

php dns file-get-contents

12
推荐指数
2
解决办法
2万
查看次数

更快的fopen或file_get_contents?

我正在运行多个流量高的网站,作为一项要求,所有图像都是通过下载的image.php?id=IMAGE_ID_HERE.如果您之前曾经这样做过,那么您知道该文件将读取文件图像并使用特殊标题将其回显到浏览器.

我的问题是,服务器上的负载非常高(150-200)并且TOP命令显示了image.php的多个实例,因此image.php运行缓慢!

问题可能是fopen在将图像发送到客户端之前将图像加载到内存中.如何读取文件并直接传递?

感谢你们


UPDATE

优化代码后,尽可能使用缓存,创建CDN.几个服务器,同步方法,负载均衡器,不再需要担心请求:)

php fopen file-get-contents

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

无法使用file_get_contents找到包装器"https"

使用https:// urls调用file_get_contents()会出现以下错误:

warning: file_get_contents(): Unable to find the wrapper "https" - did you forget to enable it when you configured PHP?
Run Code Online (Sandbox Code Playgroud)

我已经阅读了10多个SO问题,所有人都说要启用 extension=php_openssl.dll

我这样做了,我还有问题......

还有什么呢?

php apache https file-get-contents

11
推荐指数
3
解决办法
3万
查看次数

file_get_contents得到错误的结果

更新资料

我解决了问题并发布了答案。但是,我的解决方案并非100%理想。我宁愿只symlinkcachewith clearstatcache(true, $target)或中删除,clearstatcache(true, $link)但那不起作用。

我还宁愿首先避免对符号链接进行缓存,也可以在生成符号链接后立即将其从缓存中删除。不幸的是,我没有运气。由于某些原因clearstatcache(true),创建符号链接后无法正常工作,但仍会对其进行缓存。

我会很高兴地将悬赏奖励给任何能够改善我的答案并解决这些问题的人。

编辑

我试图通过每次clearstatcache运行时生成一个文件来优化我的代码,因此我只需要为每个符号链接清除一次缓存。由于某些原因,这不起作用。clearstatcache每次symlink在路径中包含a时都需要调用它,但是为什么呢?必须有一种方法可以优化我拥有的解决方案。


我使用PHP 7.3.5nginx/1.16.0。有时file_get_contents在使用时会返回错误的值symlink。问题是删除并重新创建符号链接后,其旧值保留在缓存中。有时返回正确的值,有时返回旧值。它似乎是随机的。

我试图用以下方法清除缓存或防止缓存:

function symlink1($target, $link)
{
    realpath_cache_size(0);
    symlink($target, $link);
    //clearstatcache(true);
}
Run Code Online (Sandbox Code Playgroud)

我并不是真的想要禁用缓存,但是我仍然需要100%的file_get_contents精度。

编辑

我无法发布我的源代码,因为它太长且太复杂了,所以我创建了一个最小的,可重现的示例(index.php),该示例再次出现了问题:

<h1>Symlink Problem</h1>
<?php
    $dir = getcwd();
    if (isset($_POST['clear-all']))
    {
        $nos = array_values(array_diff(scandir($dir.'/nos'), array('..', '.')));
        foreach ($nos as $no)
        {
            unlink($dir.'/nos/'.$no.'/id.txt');
            rmdir($dir.'/nos/'.$no);
        }
        foreach (array_values(array_diff(scandir($dir.'/ids'), array('..', '.'))) as $id)
            unlink($dir.'/ids/'.$id);
    }
    if …
Run Code Online (Sandbox Code Playgroud)

php symlink caching nginx file-get-contents

11
推荐指数
1
解决办法
512
查看次数

标签 统计

file-get-contents ×10

php ×10

curl ×2

apache ×1

caching ×1

curl-multi ×1

dns ×1

fopen ×1

get ×1

html-parsing ×1

http ×1

https ×1

json ×1

nginx ×1

soap ×1

soap-client ×1

symlink ×1

xml ×1