-2

I was wondering how I can get YouTube video ID what my API prints out. I want to get page content, not source code. If someone want to see how it looks, here is API URL: http://fnapi.cpt-design.tk/api.php

I have this so far

<?php $id = NULL;
      $channel_id = 'UCd_09Tp1Bk05gDqc1RWyhuw';
      $xml = simplexml_load_file(sprintf('youtube.com/feeds/videos.xml?channel_id=%s';, $channel_id));
      if (!empty($xml->entry[0]->children('yt', true)->videoId[0])){
        $id = $xml->entry[0]->children('yt', true)->videoId[0];
      }
      echo $id;
?>
David Gomes
  • 5,644
  • 16
  • 60
  • 103
  • 2
    Welcome to Stack Overflow! We're going to need more details (and more in depth explanation, also what code you have so far) of what you're really looking for. – David Gomes Jul 31 '16 at 18:15
  • @DavidGomes http://pastebin.com/iHvx3vJR It's my only code. I want to print out this id using JQuery. – chappati21 design Jul 31 '16 at 18:18

2 Answers2

0

If this page located at the same domain you can use answer by smarx.

If you want to get page content from other domain. You should use PHP => cURL

How to get page content using cURL?

Or if it's your domain you can send

<?php header("Access-Control-Allow-Origin: *"); ?>

with your response.

Community
  • 1
  • 1
ArtemSky
  • 1,173
  • 11
  • 20
  • I want get page content using JQuery :/ Not cURL. I can't use it because I'm creating browser extension. – chappati21 design Jul 31 '16 at 18:21
  • if i understand, you want to get youtube.com page content, then you can't get page content using javascript. XSS protection. Cross site contents can not be read by javascript. No major browser will allow you that. Or if you want to get youtube video id from this api, and its your domain, you should send allow-cross-origin header with your response – ArtemSky Jul 31 '16 at 18:24
  • Yeah! That's it what i want! I want to access my own page and get it's content. There's only YouTube video ID. Do you know how to send that header and get this ID? – chappati21 design Jul 31 '16 at 18:39
0

From a page on the same domain, this should work:

$.get('http://fnapi.cpt-design.tk/api.php').done(function (id) {
  console.log(id);
});

From another domain, you'll first have to modify the API to support CORS by adding a header Access-Control-Allow-Origin: *.

In PHP, this means putting this line at the top of your code:

header("Access-Control-Allow-Origin: *");
user94559
  • 59,196
  • 6
  • 103
  • 103
  • It's on different domain, because I'm creating browser extension. – chappati21 design Jul 31 '16 at 18:21
  • Well, the API doesn't support CORS, so it can't be called from JavaScript on another domain. That said, I think browser extensions can typically make cross-origin requests anyway... what browser are you writing an extension for? And is this JavaScript you're injecting into a page or code that runs in the background? You might want to share your actual code. – user94559 Jul 31 '16 at 18:25
  • I am creating extension for Opera. Here's my actual code: http://pastebin.com/T7KSGqRb And - Yes, that code runs in the background. – chappati21 design Jul 31 '16 at 18:36
  • If you own the API, the easiest thing is probably just to add CORS support. (Add `Access-Control-Allow-Origin: *`.) – user94559 Jul 31 '16 at 18:45