【发布时间】:2015-11-01 17:21:29
【问题描述】:
我一直在环顾四周,希望能找到我遇到的问题……但是我找不到。我在 PHP 中创建了一个类,在该类中有一个连接到检索 JSON 数据的 API 的函数(这个函数已经过测试,并且工作起来就像一个魅力)。但是现在我正在尝试将收到的 JSON 数据的对象分离到不同的函数中。
我现在只能通过 foreach 和 echo 解析数据,但经常这样做会很麻烦。
这是它目前的样子,所以你有一个想法。
编辑
问题已解决,public function foo(); Had 和 $info 变量有一个 json_decode();,它中断了下一个拆分数据的函数。
class parseData{
var $name;
var $domain;
public function foo($name, $domain)
{
$this->name = $name;
$this->domain = $domain;
$ch = curl_init();
$user_agent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31';
$request_headers = array();
$request_headers[] = 'User-Agent: ' . $user_agent;
$request_headers[] = 'Accept: text/html,application/xhtml+xml,application/xml,application/json;q=0.9,*/*;q=0.8';
curl_setopt($ch, CURLOPT_URL, "https://www.xxx." . $domain . "/api/public/users?name=" . $name . "");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, "https://www.xxx." . $domain . "/api/public/users?name=" . $name);
$id = json_decode(curl_exec($ch));
if (isset($id) && $id->profileVisible == 1) {
curl_setopt($ch, CURLOPT_URL, "https://www.xxx." . $domain . "/api/public/users/" . $id->uniqueId . "/profile");
$info = curl_exec($ch);
} else
$info = false;
curl_close($ch);
return $info;
}
public $response = array(), $user = array(), $friends = array(), $groups = array(), $rooms = array(), $badges = array();
public function __construct(){
$this->response = $this->foo("user", "nl");
$this->init();
}
// this function will split data into sub variables
private function init(){
$this->response =json_decode(file_get_contents($this->response), TRUE);
$this->user = $this->response['user'];
$this->friends = $this->response['friends'];
$this->groups = $this->response['groups'];
$this->rooms = $this->response['rooms'];
$this->badges = $this->response['badges'];
// free main response object
unset($this->response);
}
public function uid(){
echo $this->user['uniqueId'];
}
public function name(){
echo $this->user['name'];
}
public function membersince(){
echo $this->user['memberSince'];
}
public function motto(){
echo $this->user['motto'];
}
public function figure(){
echo $this->user['figureString'];
}
//this function will manipulate USER data.
public function user(){
echo "Naam: ".$this->user['name'].'<br/>';
echo "Motto: ".$this->user['motto'].'<br/>';
echo "Lid sinds: ".$this->user['memberSince'].'<br/>';
echo "Unique ID: ".$this->user['uniqueId'].'<br/>';
echo "figureString: ".$this->user['figureString'].'<br/>';
foreach($this->user['selectedBadges'] as $selectedBadge){
echo 'Badge index: '. $selectedBadge['badgeIndex'];
echo 'Badge code: '. $selectedBadge['code'];
echo 'Badge naam: '. $selectedBadge['name'];
echo 'Badge beschrijving: '. $selectedBadge['description'];
}
}
public function friends(){
//do stuff with $this->friends.
}
public function groups(){
//do stuff with $this->groups
}
//and other functions like badges and rooms etc.
}
$parser = new parseData();
$parser->user();
API 会发回不同的对象,我想将它们分成不同的functions,如“用户”、“朋友”、“组”等,以便从这些对象中检索所有字符串。所以我的问题是,是否可以通过同一类中的不同函数解析 API 数据?如果是这样,我该怎么做?调用该函数是否也会变得更容易,还是会像使用foreach 方法一样麻烦?
【问题讨论】:
-
请在你的输出中包含 sn-p
-
JSON 数据的输出?
-
是的,JSON 数据的输出
-
为了保持匿名,我编辑了一些结果,但结构是相同的。
{"user":{"uniqueId":"aaaa-662628f1fb34cbf4346103dff1685508","name":"foo","slogan":"Safety has no time-out!","memberSince":"2004-07-08T04:56:03.000+0000","profileVisible":true,"lastWebAccess":null} -
您想在主键
{"user":的基础上分离响应,对吗?因为可能有“朋友”、“群组”等,你想从特定的函数中调用,比如如果用户,调用 user(),如果是朋友,调用friends(),对吧?
标签: php json function class parsing