【问题标题】:Outputting specific lines of curl output输出特定行的 curl 输出
【发布时间】:2019-02-26 09:49:35
【问题描述】:

我只想显示 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 ?>

【问题讨论】:

标签: php nginx php-5.6


【解决方案1】:

您不会从谷歌金融获得任何数据,因为它是受 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

【讨论】:

  • 太好了,我现在明白了!
猜你喜欢
  • 2015-09-16
  • 2018-12-05
  • 2017-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多