How to output "name" "location" "address" to the local txt file-php Tutorial

Source: Internet
Author: User
How to output "name", "location", and "address" to the local txt file? $ Url = 'http: // api.map.baidu.com/place/v2/search? & Query = % E9 % 93% B6 % E8 % A1 % 8C & location = 39.915, 116.404 & radius = 2000 & output = json & ak = kvfo779F6gthGiGq4DFnOGHn ';
$ Html = file_get_contents ($ url );
// $ Txt = file ($ html );
Echo $ html;
File_put_contents('a1.txt ', $ html );
?>
Now I can only output all the information to the local txt file! I just don't know how to determine how to output the three messages to the local txt! Guidance


Reply to discussion (solution)

$url = 'http://api.map.baidu.com/place/v2/search?&query=%E9%93%B6%E8%A1%8C&location=39.915,116.404&radius=2000&output=json&ak=kvfo779F6gthGiGq4DFnOGHn';$html = file_get_contents($url);$json = json_decode($html, 1);$d = array('name' => '', 'location' => '', 'address' => '');foreach($json['results'] as $t) {  $res[] = array_intersect_key($t, $d);}file_put_contents('a1.txt', json_encode($res));

$url = 'http://api.map.baidu.com/place/v2/search?&query=%E9%93%B6%E8%A1%8C&location=39.915,116.404&radius=2000&output=json&ak=kvfo779F6gthGiGq4DFnOGHn';$html = file_get_contents($url);$json = json_decode($html, 1);$d = array('name' => '', 'location' => '', 'address' => '');foreach($json['results'] as $t) {  $res[] = array_intersect_key($t, $d);}file_put_contents('a1.txt', json_encode($res));

+ 1

Thank you very much! But why did I output a bunch of garbled characters? [{"Name": "\ u4e2d \ u56fd \ u5de5 \ u5546 \ u94f6 \ u884c (\ u548c \ u5e73 \ u95e8 \ u5185 \ u652f \ u884c)", "location ": {"lat": 39.90742, "lng": 116.390732}, "address ": "\ u5317 \ u4eac \ u5e02 \ u897f \ u57ce \ signature \ u5317 \ u65b0 \ signature \ u8857 \ signature \ u6811 \ u80e1 \ u540c \ u753231 \ u53f7 "}, {"name": "\ u4e2d \ u56fd \ u5de5 \ u5546 \ u94f6 \ u884c (\ u4e1c \ u4ea4 \ u6c11 \ u5df7 \ u50a8 \ u84c4 \ u6240 )", "location ".....

$url = 'http://api.map.baidu.com/place/v2/search?&query=%E9%93%B6%E8%A1%8C&location=39.915,116.404&radius=2000&output=json&ak=kvfo779F6gthGiGq4DFnOGHn';$html = file_get_contents($url);$json = json_decode($html, 1);$d = array('name' => '', 'location' => '', 'address' => '');foreach($json['results'] as $t) {  $res[] = array_intersect_key($t, $d);}file_put_contents('a1.txt', json_encode($res));



Thank you very much! But why did I output a bunch of garbled characters? [{"Name": "\ u4e2d \ u56fd \ u5de5 \ u5546 \ u94f6 \ u884c (\ u548c \ u5e73 \ u95e8 \ u5185 \ u652f \ u884c)", "location ": {"lat": 39.90742, "lng": 116.390732}, "address ": "\ u5317 \ u4eac \ u5e02 \ u897f \ u57ce \ signature \ u5317 \ u65b0 \ signature \ u8857 \ signature \ u6811 \ u80e1 \ u540c \ u753231 \ u53f7 "}, {"name": "\ u4e2d \ u56fd \ u5de5 \ u5546 \ u94f6 \ u884c (\ u4e1c \ u4ea4 \ u6c11 \ u5df7 \ u50a8 \ u84c4 \ u6240 )", "location ".....

Handle it

function foo(&$v) { $v = urlencode($v); }array_walk_recursive($res, 'foo');echo urldecode(json_encode($res));

Handle it

function foo(&$v) { $v = urlencode($v); }array_walk_recursive($res, 'foo');echo urldecode(json_encode($res));



Now the webpage is correctly displayed! However, the txt file is garbled!
[{"Name ": "% E4 % B8 % AD % E5 % 9B % BD % E5 % B7 % A5 % E5 % 95% 86% E9 % 93% B6 % E8 % A1 % 8C % 28% E5 % 92% 8C % E5 % B9 % B3 % E9 % 97% A8 % E5 % 86% 85% E6 % 94% AF % E8 % A1 % 8C % 29 ", "location": {"lat": "39.90742", "lng": "116.390732"}, "address ": "% E5 % 8C % 97% E4 % BA % AC % E5 % B8 % 82% E8 % A5 % BF % E5 % 9F % 8E % E5 % 8C % BA % E5 % 8C % 97% E6 % 96% B0 % E5 % 8D % 8E % E8 % A1 % 97% E4 % B8 % 9C % E6 % 9D % BE % E6 % A0 % 91% E8 % 83% A1 % E5 % 90% 8C % E7 % 94% B231 % E5 % 8F % B7 "}, {"name ": "% E4 % B8 % AD % E5 % 9B % BD % E5 % B7 % A5 % E5 % 95% 86% E9 % 93% B6 % E8 % A1 % 8C % 28% E4 % B8 % 9C % E4 % BA % A4 % E6 % B0 % 91% E5 % B7 % B7 % E5 % 82% A8 % E8 % 93% E6 % 84% 89% 29 ", "location": {"lat": "39.908091", "lng": "116.413558 "},"

Didn't you save the echo results?

function foo(&$v) { $v = urlencode($v); }array_walk_recursive($res, 'foo');$t = urldecode(json_encode($res));file_put_contents('a1.txt', $t);


To be honest, it is not suitable for programmers.

Didn't you save the echo results?

function foo(&$v) { $v = urlencode($v); }array_walk_recursive($res, 'foo');$t = urldecode(json_encode($res));file_put_contents('a1.txt', json_encode($t));


To be honest, it is not suitable for programmers.


I tried the method you said! I have tried other things on the Internet to avoid garbled text! Garbled! I am new to PHP, so I don't understand much! Sorry for the poor performance!


Didn't you save the echo results?

function foo(&$v) { $v = urlencode($v); }array_walk_recursive($res, 'foo');$t = urldecode(json_encode($res));file_put_contents('a1.txt', json_encode($t));


To be honest, it is not suitable for programmers.


I tried the method you said! I have tried other things on the Internet to avoid garbled text! Garbled! I am new to PHP, so I don't understand much! Sorry for the poor performance!



In this way, the above one more json_encode.

  '', 'location' => '', 'address' => '');foreach($json['results'] as $t) {  $res[] = array_intersect_key($t, $d);}function foo(&$v) { $v = urlencode($v); }array_walk_recursive($res, 'foo');$t = urldecode(json_encode($res));file_put_contents('a1.txt', $t);?>



Didn't you save the echo results?

function foo(&$v) { $v = urlencode($v); }array_walk_recursive($res, 'foo');$t = urldecode(json_encode($res));file_put_contents('a1.txt', json_encode($t));


To be honest, it is not suitable for programmers.


I tried the method you said! I have tried other things on the Internet to avoid garbled text! Garbled! I am new to PHP, so I don't understand much! Sorry for the poor performance!



In this way, the above one more json_encode.

  '', 'location' => '', 'address' => '');foreach($json['results'] as $t) {  $res[] = array_intersect_key($t, $d);}function foo(&$v) { $v = urlencode($v); }array_walk_recursive($res, 'foo');$t = urldecode(json_encode($res));file_put_contents('a1.txt', $t);?>



Thank you! The code should be okay. I think there is a problem with the encoding of my file settings! But thank you very much!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.