Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using Wordpress http api to get a server response code from youtube api so I check the validity of a video. I do this in a function from my functions.php which processes my post form where I collect the url of the youtube video. The issue is I'm getting inconsistent responses as where videos that are valid are returning 403's occasionally.

Now I also check the validity on the front end using jquery .ajax() using the same youtube api request and that works correctly 100% of the time. So a video will validate using youtube api and jquery and then sometimes return a 403 using the php method. So what is the issue here? This is my processing function for the form. You'll notice I'm printing out the $video_id every time to make sure it's correct and it is.

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' ); 
share|improve this question
add comment

1 Answer

Disclaimer: I can't give you a real explanation why this happens. I investigated this issue multiple times and (like you) didn't even get a consistent error behavior. Anyway: Every sort of RESTful remote request using a remote API is painful and error prone - it simply highly depends what the counter part gives you... not.

But... sometimes the problem is the SSL certificate and secure connections simply doesn't work out.

Disable SSL verification

The (plain stupid) solution to this is to turn it off with the second argument inside the remote request API function:

wp_remote_get( $api_url, array( 'sslverify' => false ) );
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.