简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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