我正在寻找构建一个解析特定标签的 HTML 的 PHP 脚本。我一直在使用这个代码块,改编自本教程:
<?php
$data = file_get_contents('http://www.google.com');
$regex = '/<title>(.+?)</';
preg_match($regex,$data,$match);
var_dump($match);
echo $match[1];
?>
Run Code Online (Sandbox Code Playgroud)
该脚本适用于某些网站(例如上面的 google),但是当我在其他网站(例如,freshdirect)上尝试时,出现此错误:
“警告:file_get_contents(http://www.freshdirect.com)[function.file-get-contents]:无法打开流:HTTP 请求失败!”
我在 StackOverflow 上看到了很多很棒的建议,例如extension=php_openssl.dll在 php.ini 中启用。但是(1)我的 php.ini 版本中没有extension=php_openssl.dll,并且(2)当我将它添加到扩展部分并重新启动 WAMP 服务器时,每个线程,仍然没有成功。
有人会介意给我指出正确的方向吗?非常感谢!
这似乎是一个重复的问题,但事实并非如此:我通过php:/ input(1-500mb)收到几兆字节的数据,我必须保存在文件中.更多性能(服务器负载,速度)使用:
file_put_contents($filename, file_get_contents('php://input'))
Run Code Online (Sandbox Code Playgroud)
要么
$input = fopen("php://input", "r");
$temp = tmpfile();
$realSize = stream_copy_to_stream($input, $temp);
fclose($input);
$target = fopen($filename, "w");
fseek($temp, 0, SEEK_SET);
stream_copy_to_stream($temp, $target);
fclose($target);
Run Code Online (Sandbox Code Playgroud) 如何修复php警告:file_get_contents?
Warning: file_get_contents(http://192.168.1.254:9999/api/p/internal/ucenter/login?loginName=test102&password=111111) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /Applications/XAMPP/xamppfiles/htdocs/PHP_test/index.php on line 49
Run Code Online (Sandbox Code Playgroud)
这是与$ files相关的代码:
<?php
$loginNames="test102";
$passwords="111111";
$apiUrl = "http://192.168.1.254:9999/api/p/internal/ucenter/login?loginName=".$loginNames."&password=".$passwords;
$callback = file_get_contents($apiUrl);
print_r($callback);
//echo $callback;
?>
Run Code Online (Sandbox Code Playgroud) 我使用以下API来获取使用IP的国家/地区代码
http://api.hostip.info/country.php?ip=' . $IP
Run Code Online (Sandbox Code Playgroud)
示例:在Localhost上
$IP = '202.71.158.30';
//pass the ip as a parameter for follow URL it will return the country
$country_code = file_get_contents('http://api.hostip.info/country.php?ip=' . $IP);
Run Code Online (Sandbox Code Playgroud)
它在这里运作良好,并显示国家代码.
但它在服务器上显示错误
例:
$IP=$_SERVER['REMOTE_ADDR'];
$country_code = file_get_contents('http://api.hostip.info/country.php?ip=' . $IP);
Run Code Online (Sandbox Code Playgroud)
显示以下错误:
警告:file_get_contents(http://api.hostip.info/country.php?ip=101.63.xx.xxx)[function.file-get-contents]:无法打开流:/ srv/disk4/1322145中的连接被拒绝第12行/www/servername.in/app/header.php
这有什么不对吗?
今天我遇到了一种情况。
我正在使用file_get_contents从用户文件中获取令牌。
$data=file_get_contents("http://example.com/aaa.php?user=tester&akey=abcdef1234");
$dec=json_decode($data,true);
$tokenid=$dec['message']['result']['tokenid'];
Run Code Online (Sandbox Code Playgroud)
使用令牌,我将调用另一个文件以获取详细信息;
$data=file_get_contents("http://example.com/bbb.php?user=tester&token=".$tokenid);
Run Code Online (Sandbox Code Playgroud)
问题是有时我没有得到tokenid,刷新页面后我得到了它。
在aaa.php中没有问题,它的工作正常。
我怀疑php是否file_get_contents在第二秒之前不等待令牌的响应file_get_contents(asynchronous);
我也尝试过curl,但是有时我没有得到tokenid。我还没有遇到过这类问题。
为每个会话在表单上使用令牌的 CSRF 预防方法是一种流行的方法。但是,如果file_get_contentsPHP 可以获取跨域文件表单的内容,我不明白这种令牌方式如何保护--> 它可以获取表单上的令牌并使用它。
那么这种令牌方式是如何工作的呢?
我为我自己的Cloud 应用程序编写了一个REST 接口。我有一个方法getFileFromRemote($path)应该返回一个包含文件内容的 JSON 对象。不幸的是,这只在我指定的文件$path是纯文本文件时才有效。当我尝试调用图像方法或PDF状态代码为 200 但响应为空时。为了返回文件内容,我使用它file_get_contents来检索内容。
注意:我知道 ownCloud 有一个 WebDAV 接口,但我只想用 REST 来解决这个问题。
编辑 这是代码服务器端(ownCloud):
public function synchroniseDown($path)
{
$this->_syncService->download(array($path));//get latest version
$content = file_get_contents($this->_homeFolder.urldecode($path));
return new DataResponse(['path'=>$path, 'fileContent'=>$content]);
}
Run Code Online (Sandbox Code Playgroud)
第一行检索在 ownCloud 服务器上下载内容并完全正常工作。
所以,问题出在这一行
$imageString = file_get_contents($image_url);
Run Code Online (Sandbox Code Playgroud)
有空格字符的网址不起作用.但是,如果我做
$imageString = file_get_contents(urlencode($image_url));
Run Code Online (Sandbox Code Playgroud)
什么都行不通.我一直在变量中收到假.
ulr是那种:
https://s3-eu-central-1.amazonaws.com/images/12/Screenshot from 2016-04-28 18 15:54:20.png
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用从亚马逊获取页面的内容,file_get_html()但输出带有奇怪的字符echo。谁能解释一下我该如何解决这个问题?
我还在 Stack Overflow 上发现了以下两个相关问题,但它们并没有解决我的问题。:)
这是我的代码:
$options = array(
'http'=>array(
'header'=>
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n".
"Accept-language: en-US,en;q=0.5\r\n" .
"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6\r\n"
)
);
$context = stream_context_create($options);
$amazon_url = 'https://www.amazon.com/my-url';
$amazon_html = file_get_contents($amazon_url, false, $context);
Run Code Online (Sandbox Code Playgroud)
这是我得到的输出:
??T]o?6}??`???0???-??"[?bh?tN?b0??.%%?$P??@?(??? ??????F#????A?
Run Code Online (Sandbox Code Playgroud)
大约 115k 这样的字符显示在浏览器窗口中。
这些是我的新标题:
$options = array(
'http'=>array(
'header'=>
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n".
"Accept-language: en-US,en;q=0.5\r\n"
)
);
Run Code Online (Sandbox Code Playgroud)
使用 cURL 会解决这个问题吗?
更新:
我试过卷曲。仍然得到垃圾输出。这是我的响应标头:
HTTP/1.1 200 OK
Date: Sun, 18 …Run Code Online (Sandbox Code Playgroud) 我想从yahoo.com获取搜索结果.
但是 file_get_contents()将UTF-8字符集(charset,雅虎使用的)内容转换为ISO-8859-1.
尝试:
$filename = "http://search.yahoo.com/search;_ylt=A0oG7lpgGp9NTSYAiQBXNyoA?p=naj%C5%A1%C5%A5astnej%C5%A1%C3%AD&fr2=sb-top&fr=yfp-t-701&type_param=&rd=pref";
echo file_get_contents($filename);
Run Code Online (Sandbox Code Playgroud)
脚本为
header('Content-Type: text/html; charset=UTF-8');
Run Code Online (Sandbox Code Playgroud)
要么
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Run Code Online (Sandbox Code Playgroud)
要么
$er = mb_convert_encoding($filename , 'UTF-8');
Run Code Online (Sandbox Code Playgroud)
要么
$s2 = iconv("ISO-8859-1","UTF-8",$filename );
Run Code Online (Sandbox Code Playgroud)
要么
echo utf8_encode(file_get_contents($filename));
Run Code Online (Sandbox Code Playgroud)
没有帮助,因为在获取网页内容特殊字符作为šťž被替换为问号???
我将不胜感激任何帮助.