php - 输出特定行的 curl 输出
问题描述
我只想显示某些 curl 输出行(例如,第 15-20 行)。如果我将输出写入 txt 文件,我知道如何只输出 1 行。但是如何在不先输出到文本的情况下做到这一点呢?以及如何选择多行?
如果 $myFile = "/whatever.txt" ,这仅适用于第 22 行。但不使用 curl 变量。
谢谢你。
<?php
$handle = curl_init();
$url = "https://finance.yahoo.com/";
// Set the url
curl_setopt($handle, CURLOPT_URL, $url);
// Set the result output to be a string.
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($handle);
curl_close($handle);
//echo $output;
$myFile = "$output";
$lines = file($myFile);//file in to an array
echo $lines[22]; // displays line 23 ?>
解决方案
好吧,你没有从谷歌金融那里得到任何数据,因为它是受 ssl 保护的。因此,您必须添加必要的 ssl 选项来 curl 以获取文本。请参阅更正后的代码。
$handle = curl_init();
$url = "https://finance.yahoo.com/";
// Set the url
curl_setopt($handle, CURLOPT_URL, $url);
// Set the result output to be a string.
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, 2);
$output=curl_exec($handle);
curl_close($handle);
$myFile = "myfile.txt";
file_put_contents($myFile,$output);
$lines = file($myFile);//file in to an array
echo $lines[114]; // displays line 114 which has some text
推荐阅读
- json - jq + 替换 json 中重读 56 次的单词的正确方法是什么
- python - 根据其他列中的值更新列值
- python - 反引号/反引号键在使用触发自定义热键后不起作用
- r - 使用 Plotly 将多项式回归平面添加到 3D 散点图
- java - 获取 com.google.gwt.storage.client.Storage.getSessionStorageIfSupported() 的 UnsupportedOperationException
- python - 如何在 Python 中使用 Selenium 和 Firefox 下载文件?
- rust - 如何通过查看 rust 中的 Trait 定义来正确编写返回类型
- node.js - 已编译的节点项目如何处理内部文件更改?
- php - 在while循环php中获取最低值
- group-by - 如何对包含逗号分隔字符串的列中的出现次数进行计数和分组