输出curl输出的特定行

时间:2018-09-22 02:24:57

标签: php nginx php-5.6

我只想显示卷曲输出的某些行(例如,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 ?>

1 个答案:

答案 0 :(得分:1)

由于您的ssl受保护,因此您没有从Google财经获取任何数据。因此,您必须添加必要的ssl选项以进行卷曲以获取文本。查看更正的代码。

$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
相关问题