- This topic is empty.
-
AuthorPosts
-
June 12, 2013 at 5:42 pm #45479
asiek
ParticipantI have a custom field for video format posts, where you paste the video’s ID in. Then I have a PHP function to grap the View count for youtube videos.
I tried placing:
ID, ‘v_url’, true); ?>
into
$video_ID = ‘your-video-ID’;
$JSON = file_get_contents(“https://gdata.youtube.com/feeds/api/videos/{$video_ID}?v=2&alt=json”);
$JSON_Data = json_decode($JSON);
$views = $JSON_Data->{‘entry’}->{‘yt$statistics’}->{‘viewCount’};
echo $views;
?>Where “your-video-ID” is located.
Here is what the full code looked like afterwards
$video_ID = ‘ID, ‘v_url’, true); ?>’;
$JSON = file_get_contents(“https://gdata.youtube.com/feeds/api/videos/{$video_ID}?v=2&alt=json”);
$JSON_Data = json_decode($JSON);
$views = $JSON_Data->{‘entry’}->{‘yt$statistics’}->{‘viewCount’};
echo $views;
?>Then I get this error message:
“**Parse error: syntax error, unexpected T_STRING in /home/example/public_html/test/wp-content/themes/example/format-video.php on line 23**”
What exactly is the issue here?
I’m not too familiar with php so I’m lost.
Please help.June 12, 2013 at 6:02 pm #138509chrisburton
ParticipantI’m no expert but perhaps it is this:
`’ID, ‘v_url’, true); ?>’`
Shouldn’t it be:
`”ID, ‘v_url’, true); ?>”`
June 12, 2013 at 6:27 pm #138513asiek
Participant@chrisburton I’ve tried that :/
The error disappears but the View Count doesn’t appear at all.June 12, 2013 at 6:31 pm #138514chrisburton
ParticipantThen I think something is wrong with your code that’s trying to retrieve that data. Unfortunately this isn’t my area so I’m not sure I can help further. At least removing the error helps.
Can you post a link?
June 12, 2013 at 6:45 pm #138515asiek
Participant@chrisburton Appreciate your help.
June 12, 2013 at 6:45 pm #138516Alen
ParticipantYou are already within php code block there is no need to nest `ID, ‘v_url’, true);
$JSON = file_get_contents(“https://gdata.youtube.com/feeds/api/videos/{$video_ID}?v=2&alt=json”);
$JSON_Data = json_decode($JSON);
$views = $JSON_Data->{‘entry’}->{‘yt$statistics’}->{‘viewCount’};
echo $views;?>
June 12, 2013 at 6:49 pm #138517Alen
ParticipantAlso try printing out the data that’s returned to see if anything is in fact being returned. So do something like:
ID, ‘v_url’, true);
print_r($video_ID);?>
It is possible that `$post` is not defined, try using `get_post_meta( get_the_ID(), ‘v_url’, true);`
June 12, 2013 at 6:55 pm #138508asiek
Participant@alenabdula It works now :D
And due to my lack of php knowledge I wouldn’t even know where to place the 2nd snippet you’ve posted :/
But thank you so much for your help!June 12, 2013 at 6:59 pm #138518Alen
ParticipantYou would use it where you have it now on the first line, the only diff. is the first argument! `$post->ID` to `get_the_ID()`. But if it works now, don’t fix what’s not broken :)
June 12, 2013 at 7:38 pm #138522asiek
Participant@alenabdula Thank you so much! :)
I will try theprint_r($video_ID);
, Just to check it out.I dislike the fact that the PHP function doesn’t add a coma within the views when needed though lol :( But I’m glad it works now!
June 12, 2013 at 7:49 pm #138523asiek
Participant@alenabdula Quick question. Does that line of code make sure the video id is in place? I just had a small issue where the view count didn’t appear at all. I refreshed it a few times then the view count appeared again..
June 12, 2013 at 7:52 pm #138524Alen
Participant[From php.net](http://php.net/manual/en/function.print-r.php) –> print_r — Prints human-readable information about a variable.
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.