标签: file-get-contents

从PHP变量值中删除换行符和换行符?

我有一个PHP脚本,它执行以下操作:

  • 使用file_get_contents()获取html文件的内容
  • 回声JSON对象

问题是从file_get_contents获得的值是多行的.它必须全部在一行才能采用正确的JSON格式.

例如

PHP文件:

$some_json_value = file_get_contents("some_html_doc.html");

echo "{";
echo "\"foo\":\"$some_json_value\"";
echo "}";
Run Code Online (Sandbox Code Playgroud)

生成的html文档如下所示:

{
foo: "<p>Lorem ipsum dolor 
sit amet, consectetur 
adipiscing elit.</p>"
}
Run Code Online (Sandbox Code Playgroud)

我的目标是让生成的html文档看起来像这样(值是一行,而不是三行)

{
foo: "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>"
}
Run Code Online (Sandbox Code Playgroud)

如何才能做到这一点.我意识到如果原始的html doc是一行,内容将是一行; 但是,我正试图避免这种解决方案.

更新

问题得到了正确回答.这是完整的,有效的代码:

$some_json_value = file_get_contents("some_html_doc.html");
$some_json_value = json_encode($some_json_value); // this line is the solution

echo "{";
echo "\"foo\":\"$some_json_value\"";
echo "}";
Run Code Online (Sandbox Code Playgroud)

php json file-get-contents

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

带有查询字符串的file_get_contents

我试图从PHP发送电子邮件我有一个PHP文件与所有值和其他PHP模板文件.

(两个文件都在同一台服务器上)

我正在使用file_get_contents来获取php模板文件的内容

   $url="emil_form.php";
   $a="uname";
  if(($Content = file_get_contents($url. "?uname=".$a)) === false) {
   $Content = "";
    }

   ...... EMAIL Sending Code ..........
Run Code Online (Sandbox Code Playgroud)

这里是emil_form.php的代码(电子邮件模板文件)

    Your Name is : <?php $_GET['uname']; ?>
Run Code Online (Sandbox Code Playgroud)

所以一旦我在$ Content中获得数据,我就可以通过电子邮件发送.但我收到错误无法打开文件....

我想要的是将原始php文件中的数据传递给模板php文件,以及存储在变量中的模板输出,以便我可以通过电子邮件发送.

怎么办呢?

谢谢

php email file-get-contents

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

Nginx和PHP-cgi - 不能在服务器上的任何网站的file_get_contents

这个问题最好用我认为的代码来解释.从web目录:

vi get.php
Run Code Online (Sandbox Code Playgroud)

将此php添加到get.php

<?
echo file_get_contents("http://IPOFTHESERVER/");
?>
Run Code Online (Sandbox Code Playgroud)

IPOFTHESERVER是运行nginx和PHP的服务器的IP.

php get.php
Run Code Online (Sandbox Code Playgroud)

返回在该IP BUT上托管的(默认)网站的内容

HTTP://IPOFTHESERVER/get.php

..返回504网关超时.卷曲也一样.使用PHP exec命令和GET是一样的.但是,从命令行直接它一切正常.

我在2个nginx服务器上复制了它.由于某种原因,nginx不允许我通过PHP与其运行的服务器建立HTTP连接(除非是通过命令行).

有人有任何想法吗?

谢谢!

php curl nginx file-get-contents

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

强制下载mp4文件

我想强迫用户下载YouTube视频.例如这个网址.我下载视频,我可以播放原始视频,但即使是视频的长度/大小也是相同的,我在强制下载时无法播放.

function force_download($file,$video_url)
{

  $video_data = file_get_contents($video_url);
  file_put_contents($file, $video_data);

  if(isset($file) && file_exists($file))
  {
    header('Content-length: ' .  filesize($file));
    header('Content-type: application/octet-stream');
    header('Content-Disposition: attachment; filename= "' . $file . '"');

    readfile($file);
  }
}

force_download('youtube.mp4',$video_url);
Run Code Online (Sandbox Code Playgroud)

php youtube file-get-contents readfile

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

php在html中查找字符串

我有一个html页面,我用PHP获取file_get_contents.

在html页面上,我有一个这样的列表:

<div class="results">
<ul>
    <li>Result 1</li>
    <li>Result 2</li>
    <li>Result 3</li>
    <li>Result 4</li>
    <li>Result 5</li>
    <li>Result 6</li>
    <li>Result 7</li>
</ul>
Run Code Online (Sandbox Code Playgroud)

在php文件中,我使用file_get_contents将html放在一个字符串中.我想要的是在"结果4"上搜索html.如果找到,我想知道列表项目(我希望输出为数字).

任何想法如何实现这一目标?

html php file-get-contents strpos

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

来自多个网址的file_get_contents

我想将页面内容保存到多个网址的文件中.

首先,我有来自数组的网站网址

$site = array( 
        'url' => 'http://onesite.com/index.php?c='.$row['code0'].'&o='.$row['code1'].'&y='.$row['code2'].'&a='.$row['cod3'].'&sid=', 'selector' => 'table.tabel tr'
    );  
Run Code Online (Sandbox Code Playgroud)

对于保存文件,我尝试:

foreach($site  as $n) {
$referer = 'reffername';


$header[] = "Accept: text/xml,application/xml,application/json,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header[] = "Accept-Language: en-us,en;q=0.5";

$opts = array('http'=>array('method'=>"GET",
                            'header'=>implode('\r\n',$header)."\r\n".
                            "Referer: $referer\r\n",
                            'user_agent'=> "Mozilla/5.0 (X11; U; Linux i686; pl-PL; rv:1.9.0.2) Gecko/2008092313 Ubuntu/9.25 (jaunty) Firefox/3.8"));
$context = stream_context_create($opts);

$data = file_get_contents($site["url"], false, $context);

$file = md5('$id');

file_put_contents($file, $data);
$content = unserialize(file_get_contents($file));
}
Run Code Online (Sandbox Code Playgroud)

php arrays url save file-get-contents

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

file_get_contents没有缓存?

我正在使用file_get_contents()从外部网站加载动态图像.

问题是图像已在远程网站上更新,但我的脚本仍然显示旧图像.我假设服务器在某处缓存图像,但是如何在使用file_get_contents获取文件时强制服务器清除缓存并使用更新的图像?

在我的本地机器上,我必须按CTRL + F5强制刷新图像.

我还尝试在我的脚本中添加无缓存标头,但它不起作用:

    $image = imagecreatefromstring(file_get_contents($path));
    header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
    header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date dans le passé
header('Content-type: image/png');
imagepng($image);
exit();
Run Code Online (Sandbox Code Playgroud)

php caching image file-get-contents

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

file_get_contents,HTTP请求失败

我试图通过使用从另一个站点获取内容,file_get_contents但我总是"无法打开流:HTTP请求失败!" allow_url_fopen已启用,我已关闭防火墙进行了测试.但它仍然会发生,还有其他原因吗?

编辑:这是完整的消息警告:file_get_contents(http://yemto.deviantart.com/):无法打开流:HTTP请求失败!在第15行的D:\ xampp\htdocs\deviantart\webcam\img.php中

和代码

//Load the page into a DOMDocument
$file = file_get_contents(strToLower("http://".$user.".deviantart.com/"));//TO-DO <-- if deivantart name is empty, it crashes
$doc = new DOMDocument();
@$doc -> loadHTML($file);
Run Code Online (Sandbox Code Playgroud)

php httprequest file-get-contents

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

Get-Content的PowerShell问题

我有一个问题.当我运行命令时:

powershell -command"gc C:\ Program Files\Microsoft SQLServer\MSSQL.1\MSSQL\LOG\ERRORLOG -totalcount 5

有一个错误:

"Get-Content:找不到接受参数'Files\Microsoft'的位置参数.在行:1 char:3 + gc <<<< C:\ Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\LOG\ERRORLOG -to talcount 5 + CategoryInfo:InvalidArgument :( :) [Get-Content],ParameterBindingException + FullyQualifiedErrorId:PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetContentCommand"

你能帮帮我吗?

windows powershell remote-access file-get-contents windows-console

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

file_get_contents()失败,URL中带有特殊字符

我需要从瑞典字母中获取一些包含某些字符的URL。

如果您以这样的字符串作为示例,https://en.wikipedia.org/wiki/Åland_Islands则将其file_get_contents作为参数直接传递给调用即可。但是,如果您首先运行该URL urlencode,则调用将失败,并显示以下消息:

无法打开流:没有这样的文件或目录

尽管有文档file_get_contents说:

注意:如果要使用特殊字符(例如空格)打开URI,则需要使用urlencode()对URI进行编码。

因此,例如,如果您运行以下代码:

error_reporting(E_ALL);
ini_set("display_errors", true);

$url = urlencode("https://en.wikipedia.org/wiki/Åland_Islands");

$response = file_get_contents($url);
if($response === false) {
    die('file get contents has failed');
}
echo $response;
Run Code Online (Sandbox Code Playgroud)

您将收到错误。如果您只是从代码中删除“ urlencode”,它将正常运行。

我面临的问题是URL中有一个参数是从提交的表单中获取的。而且由于PHP始终通过来运行提交的值urlencode,所以我构造的URL中的瑞典语字符将导致发生错误。

我该如何解决?

php file-get-contents

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