首页 > 解决方案 > 输出特定行的 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 ?>

标签: phpnginxphp-5.6

解决方案


好吧,你没有从谷歌金融那里得到任何数据,因为它是受 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

推荐阅读