0
好吧,我对wordpress中的ajax毫无希望,所以希望你能帮助我。我试图从抽搐中检索数据,然后每5秒更新一次。我将要展示的代码确实有效,只是不在wordpress中,因为它们以不同的方式处理ajax。这里是我的PHP代码 -在WordPress中使用Ajax更新内容
<?php
// setting the options
$twitch_username = 'ChinchillaDave';
$twitch_username = strtolower($twitch_username);
// fetching the twitch stream data | $twitch_stream
$tsh = curl_init(); // twitch stream handler
$tsh_url = "https://api.twitch.tv/kraken/streams/$twitch_username";
curl_setopt($tsh, CURLOPT_HEADER, false);
curl_setopt($tsh, CURLOPT_URL, $tsh_url);
curl_setopt($tsh, CURLOPT_RETURNTRANSFER, true);
curl_setopt($tsh, CURLOPT_SSL_VERIFYPEER, false);
$tsh_exec = curl_exec($tsh);
curl_close($tsh);
$twitch_stream = json_decode($tsh_exec, true);
?>
<?php if($twitch_stream['stream'] !== null) : ?>
<span class="alignleft">
<h3><?php echo $twitch_stream['stream']['game'] ?></h3>
<p><?php echo $twitch_stream['stream']['channel']['status'] ?></p>
</span>
<span class="alignright">
<?php
$time = strtotime($twitch_stream['stream']['created_at']);
function stream_started ($time) {
$time = time() - $time;
$tokens = array (
31536000 => 'year',
2592000 => 'month',
604800 => 'week',
86400 => 'day',
3600 => 'hour',
60 => 'min',
1 => 'sec'
);
foreach ($tokens as $unit => $text) {
if ($time < $unit) continue;
$numberOfUnits = floor($time/$unit);
return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
}
}
?>
<span class="glyphicon glyphicon-time" aria-hidden="true" style="color: #4DC47D;"></span><?php echo '<span class="hidden-xs hidden-sm">Stream Started </span>' . stream_started($time) . ' ago'; ?>
<span class="glyphicon glyphicon-play-circle" aria-hidden="true" style="color: #1a7eda;"></span><?php echo number_format($twitch_stream['stream']['average_fps']); ?><span class="hidden-xs hidden-sm"> fps</span>
<span class="glyphicon glyphicon-eye-open" aria-hidden="true" style="color: #cd201f;"></span><?php echo number_format($twitch_stream['stream']['viewers']); ?><span class="hidden-xs hidden-sm"> Viewer(s)</span>
</span>
<?php else : ?>
<span class="alignright">
Currently offline, please check back soon.
</span>
<?php endif; ?>
和我的AJAX代码 -
function twitchStreamData($) {
"use strict";
$('.twitch-stream-header').load('../wp-content/themes/creativenetwork/includes/twitch-stream-data.php'); // loads the php script that grabs the users details from Twitch
var updateInterval = setInterval(function() {
$('.twitch-stream-header').load('../wp-content/themes/creativenetwork/includes/twitch-stream-data.php'); // inserts the data into the DOM element {.twitch-stream-header}
}, 5 * 1000); // updates the php script every 5 seconds and sends it back to the DOM element
}
jQuery(document).ready(function ($) {
'use strict';
twitchStreamData($);
});
正如我已经说过了,我今儿这个,如果你能帮助我我可以通过一些自己的方式; )
喜欢这个? - var ajax_object = Object.ajax_object(ajax_url); – 2015-04-02 22:37:17
只需使用ajax_object.ajax_url就足够了。 Wordpress是如此友善,使其成为一个全局变量(由wp_localize_script引起),所以这就是你如何访问它;) – Brian 2015-04-03 05:50:58