我正在尝试通过脚本获取此网址: http: //api.alarabiya.net/sections/2/
但是收到的 JSON 响应比我直接在浏览器中打开它时要小得多,
请注意,我通过 CURL 尝试了这个 url,并设置了浏览器的相同 USER-AGENT 以及浏览器中使用的所有请求标头,但我仍然得到较小的响应。
这是仅使用 file_get_contents 的示例
<?php
echo file_get_contents("http://api.alarabiya.net/sections/2/");
?>
Run Code Online (Sandbox Code Playgroud)
我的问题是使用 file_get_contents 时是否有请求大小限制,或者 PHP 的内存无法处理它,或者到底是什么问题?
当我在 shell 中将其卷曲时,它给了我与 php 中相同的 o/p(修剪后的输出)。
您好,如何使用 file_get_contents 返回带有扩展名的文件名,这是我的代码:
$lien ="http://www.miawmiaw.com/coolimage.jpg";
$data = file_get_contents($lien);
$fichier = basename($lien);
$fp = fopen("products/".$fichier,"wb");
if (!$fp) exit;
fwrite($fp, $data);
fclose($fp);
Run Code Online (Sandbox Code Playgroud)
所以这里 $fichier 应该有文件名,例如“coolimage.jpg”
我有一个以 JSON 格式提供一些数据的服务器。我尝试用通常的方式获取这些数据:
$res = file_get_contents($url);
$result = json_decode($res);
var_dump($result);
Run Code Online (Sandbox Code Playgroud)
但 $result 还是一个字符串。问题是来自 file_get_content 的数据在数据之前有一些字母数字字符串,在数据之后有一个零。
就像是:
215ba
{"@attributes":{"ticker":"FCA"},"info...... // here all json data
0
Run Code Online (Sandbox Code Playgroud)
我已经直接从 url 检查了 json 有效性,并且格式正确,我无法理解零和 215ba 来自哪里。
显然我可以去掉字符串,消除两者,但我想知道是否有更具体的解决方案而不是解决方法
PS:不幸的是我不能使用cURL
我目前遇到一个问题,我在一个文件中有一些 php 代码并从另一个文件调用该文件。
\n\n所以:
\n\nindex.php 正在调用 file.php 并且 file.php 中有一些 php 代码。
\n\n我使用 file_get_contents 的原因是我在 index.php 中有代码,只需要读取 file.php 中由标签指定的一些内容。基于 ($_GET[\'identifier\']) 触发的节标记;file.php 中的该部分是唯一显示的部分
\n\nfile.php 中的示例代码:
\n\n<?php\n//simplified php code\nvar $item = "hello";\nvar $item2 = "Hola";\nvar $item3 = "\xe3\x81\x8a\xe3\x81\xaf\xe3\x82\x88";\n?>\n\n<section id="content">\n<?php echo $item1; ?> \n</section>\n<section id="content2">\n<?php echo $item2; ?>\n</section>\n<section id="content3">\n<?php echo $item3; ?>\n</section>\n?>\nRun Code Online (Sandbox Code Playgroud)\n\nindex.php 中的代码:
\n\n$content = file_get_contents(\'file.php\');\nif (isset($_GET[\'item\'])) {\n$section = $_GET[\'item\'];\n} else {\n$section = \'content\';\n}\n$delimiter = \'<section id="\' . $section . \'">\';\n$start = explode($delimiter, $content);\n$end = …Run Code Online (Sandbox Code Playgroud) 我做了一个表单处理并将代码分解成不同的文件以保持干净和有条理.
现在我正在设置https安全性的表单.
我拉入页面的所有文件都必须被称为https吗?在这种情况下,我不能再使用include(); 因为它不再允许相对路径?
解决方案是使用file_get_contents();? 或者只有(主)页面本身需要被称为https?
我有一个名为send.email.php的页面,它发送一封电子邮件 - 非常简单的东西 - 我传递了一个订单ID,它创建了作业请求并将其发送出去.这在我开发它的上下文中使用时工作正常(使用javascript对URL进行AJAX调用并将order_id作为查询参数传递)
我现在正试图在另一个应用程序中重用完全相同的页面,但我使用php调用它file_get_contents($base_url.'admin/send.email.php?order_id='.$order_id).当我以这种方式调用页面时,$ _SESSION数组为空isempty()= 1.
这是因为我正在使用启动一个新会话,file_get_contents并且我在登录时存储在$ _SESSION中的值是不可用的?
- >感谢您的反馈.有意义的是,新呼叫无法访问现有会话......
但是新问题:
我现在得到:无法打开流:HTTP请求失败!尝试执行时:
$opts = array('http' => array('header'=> 'Cookie: ' . $_SERVER['HTTP_COOKIE']."\r\n"));
$context = stream_context_create($opts);
$contents = file_get_contents($base_url.'admin/send.sms.php?order_id='.order_id, false, $context);
Run Code Online (Sandbox Code Playgroud)
是的,如果我将其称为以下URL,则URL工作正常:(它只是不允许我访问会话)
$result file_get_contents($base_url.'admin/send.sms.php?order_id='.$order_id);
Run Code Online (Sandbox Code Playgroud) 我试图通过php 5.2.9中的file_get_contents接收一个gzip版本的页面
我能够使用fopen使用以下代码执行此操作:
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Accept-Encoding: gzip\r\n"
)
);
$context = stream_context_create($opts);
ob_start();
$fp = fopen('http://example.com', 'r', false, $context);
fpassthru($fp);
fclose($fp);
$content = ob_get_contents();
ob_end_clean();
Run Code Online (Sandbox Code Playgroud)
这是有效的,但我希望有一种方法可以使用file_get_contents代替它.
谢谢.
无法真正弄清楚如何简化这一点.我想要的是获取这几页的内容并在我的网站上显示.是正确的方法只是为我想要的每个页面重复file_get_contents,或者我可以用另一种方式(也许是一个file_get_contents函数中的所有网站?).此外,其余的代码只是重复自我.我这整个代码只是减慢了我的网站.如果有人愿意伸出援助之手:),非常感谢你.
<?php
//Obtain Quote Info - This collects the Microsoft Stock Info
$quote10 = file_get_contents('http://finance.google.com/finance/info?client=ig&q=NYSE:GLD');
$quote9 = file_get_contents('http://finance.google.com/finance/info?client=ig&q=INDEXEURO:PX1');
$quote8 = file_get_contents('http://finance.google.com/finance/info?client=ig&q=INDEXDJSTOXX:SX5E');
$quote7 = file_get_contents('http://finance.google.com/finance/info?client=ig&q=INDEXFTSE:UKX');
$quote4 = file_get_contents('http://finance.google.com/finance/info?client=ig&q=INDEXNIKKEI:NI225');
$quote5 = file_get_contents('http://finance.google.com/finance/info?client=ig&q=SHA:000001');
$quote4 = file_get_contents('http://finance.google.com/finance/info?client=ig&q=INDEXDJX:.DJI');
$quote3 = file_get_contents('http://finance.google.com/finance/info?client=ig&q=INDEXNASDAQ:.IXIC');
$quote2 = file_get_contents('http://finance.google.com/finance/info?client=ig&q=INDEXSP:.INX');
$quote = file_get_contents('http://finance.google.com/finance/info?client=ig&q=NASDAQ:GOOG');
$quote1 = file_get_contents('http://finance.google.com/finance/info?client=ig&q=NASDAQ:YHOO');
//Remove CR's from ouput - make it one line
$json = str_replace("\n", "", $quote);
$json1 = str_replace("\n", "", $quote1);
$json2 = str_replace("\n", "", $quote2);
$json3 = str_replace("\n", "", $quote3);
$json4 = str_replace("\n", "", $quote4);
$json5 …Run Code Online (Sandbox Code Playgroud) 可能重复:
在PHP中可靠地发送大文件
我将分发一个PHP脚本,使人们可以通过掩码URL从服务器向他们的客户提供可下载的产品(不想泄露服务器上的位置).有些文件可能非常大,所以我需要确保使用能够处理更大文件的方法.
我熟悉如何利用以下每种下载方法,我只想问哪个是最好的(最有效,最可靠,普遍支持等):
流fopen
流式cURL
的file_get_contents
卷曲
我想从他的文件中删除所有用户名和密码并输出得很好.我在我的计算机上的appserv 2.5.1上编写了一个代码,但只有最后一个循环给出了用户名输出.在其他机器上测试代码,它完美地工作.不知道是什么问题......
usernames.txt内容:
user1
user2
user3
Run Code Online (Sandbox Code Playgroud)
passwords.txt内容:
pass1
pass2
pass3
Run Code Online (Sandbox Code Playgroud)
脚本内容:
$usernames = explode("\n", file_get_contents("usernames.txt"));
$passwords = explode("\n", file_get_contents("passwords.txt"));
foreach( $usernames as $username )
{
foreach( $passwords as $password )
{
echo $username.":".$password."\n";
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
:pass1
:pass2
:pass3
:pass1
:pass2
:pass3
user3:pass1
user3:pass2
user3:pass3
Run Code Online (Sandbox Code Playgroud)