我正在使用Wordpress http api 从youtube api获取服务器响应代码,以便检查视频的有效性。我在我的函数中的函数中执行此操作。php处理我的帖子表单,我在这里收集youtube视频的url。问题是我得到的回应不一致,因为有效的视频偶尔会返回403。
现在,我还使用jquery检查前端的有效性。ajax()使用相同的youtube api请求,并且在所有情况下都能正常工作。因此,视频将使用youtube api和jquery进行验证,然后有时使用php方法返回403。那么这里的问题是什么?这是我对表单的处理函数。你会注意到我每次都打印$video\\u id,以确保它是正确的。
function video_process_form( $query ) {
global $error_output;
if ( $query->is_page( \'submit-video\' ) && isset( $_POST[\'title\'] )) {
$output = null;
if ( ! function_exists( \'wp_handle_upload\' )) {
require_once(ABSPATH . "wp-admin" . \'/includes/image.php\');
require_once(ABSPATH . "wp-admin" . \'/includes/file.php\');
require_once(ABSPATH . "wp-admin" . \'/includes/media.php\');
}
$file = $_FILES;
// Do some minor form validation to make sure there is content
if ($_POST[\'title\'] != null && $_POST[\'title\'] != \'Video Title\') {
$title = $_POST[\'title\'];
} else {
$error_output .= \'Please enter a video title<br/>\';
}
if ($_POST[\'description\'] != null && $_POST[\'description\'] != \'Video Description\') {
$description = $_POST[\'description\'];
}
if ($_POST[\'video_url\'] != null && $_POST[\'video_url\'] != \'Youtube URL\') {
$vid_url = $_POST[\'video_url\'];
$video_id = getVideoId($vid_url);
if ($video_id) {
printf($video_id);
$video_response = wp_remote_retrieve_response_code(wp_remote_get(\'http://gdata.youtube.com/feeds/api/videos/\' .
$video_id));
printf($video_response);
if ($video_response == 200) {
$video_url = $vid_url;
} else {
$error_output .= \'This is not an existing youtube video<br/>\';
}
} else {
$error_output .= \'This is not an existing youtube video<br/>\';
}
} else {
$error_output .= \'This is not an existing youtube video<br/>\';
}
$tags = $_POST[\'post_tags\'];
// Add the content of the form to $post as an array
$new_post = array(
\'post_title\' => $title,
\'post_content\' => $description,
\'tags_input\' => array($tags),
\'post_category\' => array(12),
\'post_status\' => \'publish\', // Choose: publish, preview, future, draft, etc.
\'post_type\' => fod_videos // Use a custom post type if you want to
);
// If No Errors, Save Post and Redirect //
if ($error_output == null) {
$pid = wp_insert_post($new_post);
update_post_meta($pid,\'video_code\',$video_url);
wp_redirect( get_permalink($pid));
// If Errors, Return Errors for Display in Template //
}
}
do_action(\'wp_insert_post\', \'wp_insert_post\');
}
add_action( \'pre_get_posts\', \'video_process_form\' );