简体   繁体   English

GuzzleHttp请求文本文件而不会丢失换行符

[英]GuzzleHttp request text file without losing newlines

I'm extracting data from the National Data Buoy Center (NDBC) and I was grabbing a text file of the latest observations , which if I do this in JavaScript using AJAX I get the newlines and can parse the data readily, but using PHP (specifically GuzzleHttp) the newlines are lost and I can't expand on newlines to parse since they don't exist. 我正在从国家数据浮标中心(NDBC)提取数据,并且正在抓取一个最新观测值的文本文件,如果我使用AJAX在JavaScript中进行此操作,我将获得换行符并可以轻松地解析数据,但是可以使用PHP(尤其是GuzzleHttp),换行符丢失了,因为它们不存在,所以我无法在换行符上进行解析。

Request 请求

$url = "http://www.ndbc.noaa.gov/data/latest_obs/41004.txt";
$res = $this->httpClient->request('GET', $url);

Response 响应

Station 41004 32° 30.0' N 79° 6.0' W 2:50 am EST 0750 GMT 01/26/18 Wind: ENE (60°), 15.5 kt Gust: 19.4 kt Seas: 3.6 ft Peak Period: 5 sec Pres: 30.54 Air Temp: 55.6 °F Water Temp: 70.2 °F Dew Point: 40.5 °F Wave Summary 2:40 am EST 0700 GMT 01/26/18 Swell: 1.3 ft Period: 10.0 sec Direction: SE Wind Wave: 3.3 ft Period: 5.0 sec Direction: ENE

Instead of like: 而不是像:

Station 41004
32° 30.0' N  79° 6.0' W

2:20 am EST
0720 GMT 01/26/18
Wind: ENE (60°), 13.6 kt
Gust: 19.4 kt
Pres: 30.54
Air Temp: 55.0 °F
Water Temp: 70.3 °F
Dew Point: 40.1 °F

Wave Summary
1:40 am EST
0600 GMT 01/26/18
Swell: 1.3 ft
Period: 8.3 sec
Direction: ENE
Wind Wave: 3.3 ft
Period: 4.3 sec
Direction: NE

Is there a way to maintain the newlines characters? 有没有办法保持换行符?

I think the new lines characters are there, at least for me, using this code: 我认为,至少对于我来说,使用以下代码可以换行:

$client = new GuzzleHttp\Client();
$res = $client->request('GET', 'http://www.ndbc.noaa.gov/data/latest_obs/41004.txt');
$result = utf8_encode($res->getBody()->getContents());

Printing the result, using any of the following methods: 使用以下任何一种方法打印结果:

echo '<pre>' . $result . '</pre>';
echo str_replace("\n", '<br>', $result);

Gives me this: 给我这个:

Station 41004
32° 30.0' N  79° 6.0' W

3:30 am EST
0830 GMT 01/26/18
Wind: ENE (60°), 15.5 kt
Gust: 19.4 kt
Pres: 30.54
Air Temp: 55.8 °F
Water Temp: 70.0 °F
Dew Point: 40.1 °F

Wave Summary
2:40 am EST
0700 GMT 01/26/18
Swell: 1.3 ft
Period: 10.0 sec
Direction: SE
Wind Wave: 3.3 ft
Period: 5.0 sec
Direction: ENE
Station 41004
32° 30.0' N 79° 6.0' W

3:30 am EST
0830 GMT 01/26/18
Wind: ENE (60°), 15.5 kt
Gust: 19.4 kt
Pres: 30.54
Air Temp: 55.8 °F
Water Temp: 70.0 °F
Dew Point: 40.1 °F

Wave Summary
2:40 am EST
0700 GMT 01/26/18
Swell: 1.3 ft
Period: 10.0 sec
Direction: SE
Wind Wave: 3.3 ft
Period: 5.0 sec
Direction: ENE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM