我正在为Android应用程序创建自己的API,但我似乎无法解决这个问题。(请注意,这是我的第一个php项目)。
我试图实现的是将一个元素添加到关联数组,然后将其编码为 JSON,但我唯一实现的就是每次循环重复时对 JSON 进行编码,从而导致格式错误的 JSON。
这是带有循环的函数
function checkLike($media_id, $user1_id, $access_token){
$url = 'https://api.instagram.com/v1/media/'.$media_id.'/likes?access_token='.$access_token;
$json1 = file_get_contents($url);
$arr1 = json_decode($json1);
$arr = array('media_id' => $media_id, 'hasLiked' => 0);
if(is_array($arr1->data)){
foreach ($arr1->data as $key => $value) {
//echo 'Comprobando el id '.$value->id.' deberia ser '. $user1_id.' ';
if($value->id === $user1_id){
$arr = array('media_id' => $media_id, 'hasLiked' => 1);
}else{
echo 'nope';
}
}encode_json($arr);
}else{
echo 'false1';
}
}
这是它被调用的另一个循环
$json = file_get_contents('https://api.instagram.com/v1/users/self/media/recent/?access_token='.$token.'&count=5');
$arr = json_decode($json);
if (is_array($arr->data)) {
foreach ($arr->data as $key => $value) {
checkLike($value->id, get_user_id($user, $token), $token);
}
}
但是,我得到的 JSON 如下:
{"media_id":"1198059901794013237_483636171","hasLiked":0}
{"media_id":"1196611512178987610_483636171","hasLiked":1}
{"media_id":"1195162888588906891_483636171","hasLiked":1}
{"media_id":"1193726158308528216_483636171","hasLiked":1}
{"media_id":"1192260158916546943_483636171","hasLiked":1}
我该怎么做才能得到这样的东西?
{
"photos":[
{
"mediaID":"1198059901794013237_483636171",
"hasLiked":0
}, {
"mediaID":"1196611512178987610_483636171",
"hasLiked":1
}
]
}
每次循环都会覆盖数组。在您的循环中,按如下方式更改此行:
$arr[] = array('media_id' => $media_id, 'hasLiked' => 1);
然后$arr
将是一个数组数组,当您在循环外调用json_encode($arr)
时,您将在一个 JSON 字符串中获取所有数据。
这是完全固定的代码 - 这应该有效,但我还没有尝试过:
function checkLike($media_id, $user1_id, $access_token){
$url = 'https://api.instagram.com/v1/media/'.$media_id.'/likes?access_token='.$access_token;
$json1 = file_get_contents($url);
$arr1 = json_decode($json1);
// Initialize and add first element to array
$arrJSON[] = array('media_id' => $media_id, 'hasLiked' => 0);
if(is_array($arr1->data)){
foreach ($arr1->data as $key => $value) {
if($value->id === $user1_id){
// Add to $arrJSON
$arrJSON[] = array('media_id' => $media_id, 'hasLiked' => 1);
}else{
echo 'nope';
}
}
} else {
echo 'false1';
}
// Encode entire array as JSON and echo
echo json_encode($arrJSON);
}
只需替换此行:
json_encode($arr);
跟:
json_encode(array("photos"=>$arr));
好的,我正在尝试在函数中创建数组,但我应该在调用函数后完成。我使用该函数返回用户是否喜欢将照片作为布尔值。代码为:
检查喜欢函数
function checkLike($media_id, $user1_id, $access_token){
$url = 'https://api.instagram.com/v1/media/'.$media_id.'/likes?access_token='.$access_token;
$json1 = file_get_contents($url);
$arr1 = json_decode($json1);
$hasLiked = false;
if(is_array($arr1->data)){
foreach ($arr1->data as $key => $value) {
//echo 'Comprobando el id '.$value->id.' deberia ser '. $user1_id.' ';
if($value->id === $user1_id){
$hasLiked = true;
}else{
echo 'nope';
}
}
}else{
echo 'false';
}
return $hasLiked;
}
主代码:
$json = file_get_contents('https://api.instagram.com/v1/users/self/media/recent/?access_token='.$token.'&count=5');
$media_array= json_decode($json);
if (is_array($media_array->data)) {
foreach ($media_array->data as $key => $value) {
$mediaID = $value->id;
if(checkLike($mediaID, get_user_id($user, $token), $token)){
$arrJSON[] = array('media_id' => $mediaID, 'hasLiked' => 1);
}else{
$arrJSON[] = array('media_id' => $mediaID, 'hasLiked' => 0);
}
}
encode_json(array('photos'=>$arrJSON));
}