Hello i am makeing script to check if youtube video exsists or not
but it's dont work i tryed lot of ways but noone goes maby someone will know it

<?php



if(isset($_GET['id'])){
$check = simplexml_load_file("http://gdata.youtube.com/feeds/api/videos/".$_GET['id']);
if($check == true) {
            echo "Video Exsist";
			    return true;
        } else {
             echo "Video Dose NOT exsist";
			 return false;
        }
}
echo '
<form action="" method="get">
<input type="text" name="id" />
<input type="submit" value="Check" />
</form>';


?>

    Did you try simply entering an invalid id and seeing how the response differed from a valid id? Two things that I immediately noticed for an 'invalid' id were:

    1. The HTTP status code returned is 400 (Bad Request).

    2. The content/body of the response consisted of only 10 characters: "Invalid id"

      HERE CAN CHECK SCRIPT
      ERROR on invalid id

      Warning: simplexml_load_file(http://gdata.youtube.com/feeds/api/videos/123132) [function.simplexml-load-file]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in C:\WEB\home\lighttpd\instinkt.wos.lv\http\mod\admin\yt_check.php on line 6
      
      Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://gdata.youtube.com/feeds/api/videos/123132" in C:\WEB\home\lighttpd\instinkt.wos.lv\http\mod\admin\yt_check.php on line 6
      Video Dose NOT exsist
      

      on valid id no errors just
      Video Dose NOT exsist

        GeRik wrote:

        ERROR on invalid id

        Any HTTP status code of 4xx will cause most PHP file-reading functions to return false. Relying on this return value, however, will always cause PHP to throw E_WARNING level errors due to the "failed to open stream: HTTP request failed!" error.

        A better, non-error producing method might be to use [man]cURL[/man] instead to retrieve the response. Using cURL will allow you to detect the 400 status code and/or "Invalid id" response without causing E_WARNING level errors in PHP, though it'll require a bit more overhead in terms of writing the code to do this.

        GeRik wrote:

        on valid id no errors just
        Video Dose NOT exsist

        That's because you're trying to compare the return value of [man]simplexml_load_file/man to boolean true, which isn't what that function returns on success. A better solution would probably be to check that the return value isn't boolean false, which indicates that an error occurred. However, if you modify your code to use cURL as I suggested above, this solution probably wouldn't apply (since you'd be doing the error checking on the response obtained via cURL, not from the SimpleXML library).

          it will be not so easy bacose i never worked with cURL
          can you show me some example how i can do that 😕

            GeRik;10956621 wrote:

            can you show me some example how i can do that 😕

            The manual has many example code snippets for [man]cURL[/man] (many can be found on the [man]curl_setopt/man manual page in the user-contributed notes section). For example, on the "Using PHP's cURL module to fetch the example.com homepage" manual page (manual link: [man]curl.examples-basic[/man]), a user-contributed note from "cmnajs at gmail dot com" shows how to capture the response of a web page in a variable. Using that same code (with the URL you used in your code above), $output was set to:

            string(10) "Invalid id"

            and using [man]curl_getinfo/man to retrieve the HTTP status code returned:

            int(400)

              If your goal is to simply verify that a given id is valid/exists according to Google's API, then it would appear that you could do that just by examining the HTTP status code of the response.

              For example, at present, an invalid id returns HTTP response code 400 (Bad Response), yet all "valid" video id's return a status code of 200 (OK).

              Thus, simply use [man]curl_getinfo/man to get the status code. If it's 200, assume that the video exists. If it's anything other than 200, (e.g. in the 4xx range), then assume either the video doesn't exist or there's a problem with the remote API.

                if(isset($_GET['id'])){
                $check = "http://gdata.youtube.com/feeds/api/videos/".$_GET['id'];
                $ch = curl_getinfo(); // create cURL handle (ch)
                if (!$ch) {
                    die("Couldn't initialize a cURL handle");
                }
                // set some cURL options
                $ret = curl_setopt($ch, CURLOPT_URL,            $check);
                $ret = curl_setopt($ch, CURLOPT_HEADER,         0);
                $ret = curl_setopt($ch, CURLOPT_QUOTE,           0);
                $ret = curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
                $ret = curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
                $ret = curl_setopt($ch, CURLOPT_TIMEOUT,        30);
                
                // execute
                $ret = curl_exec($ch);
                
                if (empty($ret)) {
                    // some kind of an error happened
                    die(curl_error($ch));
                    curl_close($ch); // close cURL handler
                } else {
                    $info = curl_getinfo($ch);
                    curl_close($ch); // close cURL handler
                
                if (empty($info['http_code'])) {
                        die("No HTTP code was returned");
                } else {
                    // echo results
                	if($info['http_code'] == 200){
                	echo "<br/>Video Exsist";}else{ echo "</br>Video Dose NOT Exsist"; }
                }
                
                }
                }
                

                I useing curl_getinfo() but anyway its giving a content of xml

                  GeRik wrote:

                  I useing curl_getinfo() but anyway its giving a content of xml

                  That's because you set CURLOPT_RETURNTRANSFER to 0/false:

                  PHP Manual wrote:

                  CURLOPT_RETURNTRANSFER
                  TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.

                    BIG THX You really helped me 🙂 RESLOVED 🙂

                      Glad I could help. Don't forget to mark this thread resolved using the link on the Thread Tools menu above.

                        Write a Reply...