解析JSON数据和it';将对象转换为不同的PHP函数
我一直在四处寻找,希望能找到我遇到的问题。。。但是我找不到它。我在PHP中创建了一个类,在该类中有一个函数连接到检索JSON数据的API(该函数已经过测试,工作起来很有魅力)。然而,现在我试图将接收到的JSON数据的对象分离到不同的函数中 我现在只能通过foreach和echo来解析数据,但是经常这样做会很麻烦 这就是它目前的样子,所以你会有一个想法 编辑 问题已经解决,解析JSON数据和it';将对象转换为不同的PHP函数,php,json,function,class,parsing,Php,Json,Function,Class,Parsing,我一直在四处寻找,希望能找到我遇到的问题。。。但是我找不到它。我在PHP中创建了一个类,在该类中有一个函数连接到检索JSON数据的API(该函数已经过测试,工作起来很有魅力)。然而,现在我试图将接收到的JSON数据的对象分离到不同的函数中 我现在只能通过foreach和echo来解析数据,但是经常这样做会很麻烦 这就是它目前的样子,所以你会有一个想法 编辑 问题已经解决,公共函数foo()Had和$info变量具有json_decode()中断了下一个分割数据的函数 class parseDat
公共函数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();
类解析数据{
var$name;
var$域;
公共函数foo($name,$domain)
{
$this->name=$name;
$this->domain=$domain;
$ch=curl_init();
$user_agent='Mozilla/5.0(X11;Linux i686)AppleWebKit/537.31(KHTML,比如Gecko)Chrome/26.0.1410.43 Safari/537.31';
$request_headers=array();
$request\u headers[]='User-Agent:'。$User\u-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_头,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_解码(curl_exec($ch));
如果(isset($id)&&$id->profileVisible==1){
curl_setopt($ch,CURLOPT_URL,”https://www.xxx.“$domain.”/api/public/users/“$id->uniqueId。”/profile”);
$info=curl\u exec($ch);
}否则
$info=false;
卷曲关闭($ch);
返回$info;
}
public$response=array(),$user=array(),$friends=array(),$groups=array(),$rooms=array(),$badges=array();
公共函数构造(){
$this->response=$this->foo(“用户”、“nl”);
$this->init();
}
//此函数将数据拆分为子变量
私有函数init(){
$this->response=json\u decode(file\u get\u 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'];
//自由主响应对象
取消设置($this->response);
}
公共函数uid(){
echo$this->user['uniqueId'];
}
公共函数名(){
echo$this->user['name'];
}
公共函数membersince(){
echo$this->user['memberSince'];
}
公共职能座右铭(){
echo$this->user[“座右铭];
}
公共功能图(){
echo$this->user['figureString'];
}
//此函数将操作用户数据。
公共函数用户(){
echo“Naam:”.$this->user['name'].
;
echo“座右铭:“$this->user['moniton']”。
;
echo“Lid sinds:”.$this->user['memberSince'].
;
回显“唯一ID:”.$this->user['uniqueId']。
;
echo“figureString:”.$this->user['figureString']。
;
foreach($此->用户['selectedBadges']作为$selectedBadge){
回显“徽章索引:”.$selectedBadge['badgeIndex'];
回显“徽章代码:”.$selectedBadge[“代码”];
echo“Badge naam:”.$selectedBadge['name'];
回显“徽章名称:”.$selectedBadge[“说明”];
}
}
公益活动之友(){
//用$this->friends做一些事情。
}
公共职能小组(){
//使用$this->groups进行操作
}
//和其他功能,如徽章和房间等。
}
$parser=新的parseData();
$parser->user();
API发回不同的对象,我想把它们分成不同的函数
“user”、“friends”、“groups”等,以便从这些对象中检索所有字符串。所以我的问题是,是否可以通过同一类中的不同函数解析API数据?如果是,我该怎么做?调用函数是一项更容易的任务,还是只是像使用foreach
方法一样麻烦?因此,我已经研究了您的json
,您可以这样做
class parseData{
public $response = array(), $user = array(), $friends = array(), $groups = array(), $rooms = array(), $badges = array();
public function __construct(){
$this->response = call_your_function_get_api_response();
$this->init();
}
// this function will split data into sub variables
private function init(){
$this->response = json_decode(file_get_contents('json.json'), TRUE);
//I'm using file get contents here, as I've stored your json into file.
// but you'll have to do like this
//$this->response = json_decode($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);
}
/*
* I'm updating here with new function name(), so to print name only do this.
*/
public function name(){
echo "Name: ".$this->user['name'].'<br/>'; //This will print name only.
}
//this function will manipulate USER data.
public function user(){
echo "User's Unique ID: ".$this->user['uniqueId'].'<br/>';
echo "Name: ".$this->user['name'].'<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 name: '. $selectedBadge['name'];
echo 'Badge description: '. $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();
/*
* and you can call function like this
* $parser->friends(); will process only friends data from json response
* $parser->groups(); will process only groups data from json response.
* ... rest of functions ...
*/
另一次编辑
根据你的评论
我再次将\u construct()
代码放在这里
public function __construct(){
$get = new foo();
$this->response = $get->bar("person", "nl");
$this->init();
}
其余的事情都是一样的
$parser = new parseData();
$parser->user();
$parser->name(); //will display name only.
将所有内容放在一起
您需要更新两个函数
public function __construct(){
$this->response = json_decode($this->foo("user", "nl"), TRUE);
$this->init();
}
在init()
中注释掉第一行,因为它不再需要了
希望这能对您有所帮助。请包括您的输出片段以及JSON数据的输出?是的,JSON数据的输出为了使此人匿名,我编辑了一些结果,但结构是相同的<代码>{“用户”:{“uniqueId”:“aaaa-662628f1fb34cbf436103df1685508”,“姓名”:“foo”,“口号”:“安全无超时!”,“memberbeason”:“2004-07-08T04:56:03.000+0000”,“profileVisible”:true,“lastWebAccess”:null}
您想在主键的基础上分离响应{“用户”:
,对吗?因为可能有“朋友”、“组”等等,你想从特定的函数调用,比如if user,call user(),if friends,call friends(),对吗?谢谢!虽然我有一个问题,但如果我需要回显“name”o怎么办
public function __construct(){
$this->response = json_decode($this->foo("user", "nl"), TRUE);
$this->init();
}