使用get_file_contents从页面获取信息?
我一直在搜索整个互联网上如何获取网页的内容,然后使用这些信息并将其添加到数据库中。我终于找到了get_file_contents。除了我需要不仅能够获取全部内容,还需要能够将信息转换为变量以添加到数据库中。它还必须支持将多个人添加到数据库中。 有人可以帮助我吗? http://www.pixelhoster.net/api/m-shopping-purchases/m/4087396是页面的API的链接。使用get_file_contents从页面获取信息?
是的,这是一个非常常见的问题。 但是我感觉不错:
<?php
//Get the json string from the api
$data = file_get_contents('http://www.pixelhoster.net/api/m-shopping-purchases/m/4087396');
//Decode that string, true to decode into an array
$data = json_decode($data,true);
print_r($data);
/*Echos
Array
(
[0] => Array
(
[user] => Array
(
[user_id] => 917115
[username] => Fiskarbengtson
)
[item_name] => VIP
[item_price] => 15.00
[purchase_date] => 1330322376
[currency] => USD
[item_id] => 1639
[custom_field] =>
)
)
*/
//So to access the array
echo $data[0]['user']['username'];
echo $data[0]['item_name'];
?>
Beat me到它。 Lucky @ WillRenfroe,我的回答只是文档的链接。 – Resorath 2012-02-28 02:25:30
我只是改变[0]的值来支持多个用户?并将'用户'和'用户名'变成一个变量,我会说$用户= $数据[0] ['用户']; ? – 2012-02-28 02:32:56
我不确定你的意思是多个用户,它依赖于api返回的内容,如果你设置了'$ User = $ data [0] ['user'];'这会将数组部分'user_id && username'添加到'$ User'例如:'$ User ['username']或$ User ['user_id']'我建议你熟悉http://php.net/manual/en/language.types.array.php祝你好运, 感谢你的接纳 – 2012-02-28 02:37:58
所以你想写一个...解析器? – Resorath 2012-02-28 02:12:12
@resorath但我会怎么做? – 2012-02-28 02:12:59
这可能是Stack Overflow上最常见的问题......先搜索一下,你会发现你需要知道的一切从如何刮,如何解析(而不是正则表达式),以及如何做mysql变量。 – hackartist 2012-02-28 02:14:39