0

I'm trying to fetch YouTube thumbnails and video id's using the API v3 and echo them into HTML, however I need to echo two variables at the same time. At the moment I have the following:

<?php
$url = 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=12&playlistId=UUDWdi7vjr3ac_z_VMMeiL_Q&key=*************************';
$data = file_get_contents($url);
if ((preg_match_all("/\"videoId\":\s*\"(.+)\"/", $data, $video_get) ) && (preg_match_all("/\"title\":\s*\"(.+)\"/", $data, $title_get) ) ) {
    foreach (($video_get[1] as $videoid) && foreach ($title_get[1] as $title ) ){
        echo ("<div id=\"video-thumb\" class=\"col-sm-6\"><img id=\"video-thumb-320180\" src=\"https://i.ytimg.com/vi/'.$videoid.'/mqdefault.jpg\"><div class=\"video-hover\"><img class=\"play-hover\" src=\"images/play.png\"></div><h2 class=\"video-title\"><i><a href=\"https://youtube.com/watch?v='.$videoid.'\">'.$title.'</a></h2></i></div>");
        }
    }
else {
    echo ("Error fetching uploads!")
}
?>

However after trying this it seems I cannot use foreach as && foreach as. What's the solution here?

4
  • It would help to see a sample of the file_get_contents() output. Commented May 8, 2015 at 1:35
  • Its not possible. Try nested loops instead Commented May 8, 2015 at 1:35
  • Actually, that API returns JSON. You don't need to preg_match anything. Just use json_decode(). Commented May 8, 2015 at 1:37
  • As stated on the comment to the other answer. I'm very new to PHP. How does the json format help me and how would I get my video and title elements from it? Commented May 8, 2015 at 1:39

1 Answer 1

1

Not sure why you're trying to do preg_match() ... but that data is json, just json_decode() it and you're done.

$url = 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=12&playlistId=UUDWdi7vjr3ac_z_VMMeiL_Q&key=AIzaSyBYG6nKkdddYT2NXg987kljPrKqsCVXcNQ';
/** data is in an array **/
$data = json_decode(file_get_contents($url), true);
foreach($data['items'] as $row) {
    echo ("
    <div id=\"video-thumb\" class=\"col-sm-6\">
        <img id=\"video-thumb-320180\" src=\"https://i.ytimg.com/vi/".$row['snippet']['resourceId']['videoId']."/mqdefault.jpg\">
        <div class=\"video-hover\">
            <img class=\"play-hover\" src=\"images/play.png\">
        </div>
        <h2 class=\"video-title\">
            <i><a href=\"https://youtube.com/watch?v=".$row['snippet']['resourceId']['videoId']."\">" . $row['snippet']['title']."</a></i>
        </h2>
    </div>");
}

response (from documentation)

{
  "kind": "youtube#playlistItemListResponse",
  "etag": etag,
  "nextPageToken": string,
  "prevPageToken": string,
  "pageInfo": {
    "totalResults": integer,
    "resultsPerPage": integer
  },
  "items": [
    {
      "kind": "youtube#playlistItem",
      "etag": etag,
      "id": string,
      "snippet": {
        "publishedAt": datetime,
        "channelId": string,
        "title": string,
        "description": string,
        "thumbnails": {
          (key): {
            "url": string,
            "width": unsigned integer,
            "height": unsigned integer
          }
        },
        "channelTitle": string,
        "playlistId": string,
        "position": unsigned integer,
        "resourceId": {
          "kind": string,
          "videoId": string,
        }
      },
      "contentDetails": {
        "videoId": string,
        "startAt": string,
        "endAt": string,
        "note": string
      },
      "status": {
        "privacyStatus": string
      }
    }
  ]
}
Sign up to request clarification or add additional context in comments.

9 Comments

Sorry I'm very new to PHP. How does the json format help me and how would I get my video and title elements from it?
json stands for .. JavaScript Object Notation. You will either see that or xml for most apis, it's extremely easy to convert it into workable code.
JSON is basically a flattened (serialized) object or array. It's how most APIs return request data.
I recommend that you read this page for the full documentation, developers.google.com/youtube/v3/code_samples
here is the relevant documentation about json_decode php.net/manual/en/function.json-decode.php
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.