details.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. require_once 'lib/simple_html_dom.php';
  3. $assert_counter = 0;
  4. function check($condition, $bad_request=false) {
  5. global $assert_counter;
  6. $assert_counter++;
  7. if (!$condition) {
  8. if ($bad_request) {
  9. header('HTTP/1.1 400 Bad Request');
  10. } else {
  11. header('HTTP/1.1 500 Internal Server Error');
  12. echo 'failed to parse scraped data ' . $assert_counter;
  13. }
  14. exit;
  15. }
  16. return $condition;
  17. }
  18. // Fetch details page
  19. check(isset($_GET['id']), true);
  20. check(is_numeric($_GET['id']), true);
  21. $url = 'http://www.tvgids.nl/programma/' . $_GET['id'] . '?cookieoptin=true';
  22. $dom = file_get_html($url);
  23. $response = compact('url');
  24. foreach ($dom->find('head meta[property^=og:]') as $tag) {
  25. $key = substr($tag->property, 3);
  26. if ($key == 'title' || $key == 'description' || $key == 'url')
  27. $response[$key] = $tag->content;
  28. }
  29. if ($prog = $dom->getElementById('prog-content')) {
  30. //if ($video = $prog->getElementById('prog-video')) {
  31. // $response['media'] = $video->outertext;
  32. //} else if ($carousel = $prog->find('.owl-carousel', 0)) {
  33. // //$images = array();
  34. // //foreach ($carousel->find('div[style^=background:]') as $img) {
  35. // // check(preg_match('/^background:\s*url\(\'(.*?)\'\).*$/', $img->style, $m));
  36. // // $images[] = $m[1];
  37. // //}
  38. // $response['media'] = $carousel->outertext;
  39. //}
  40. $response['properties'] = array();
  41. foreach ($prog->find('.programmering_info_detail li') as $tag) {
  42. $response['properties'][] = array(
  43. 'name' => preg_replace('/:\s+$/', '', $tag->children(0)->plaintext),
  44. 'description' => $tag->children(1)->innertext,
  45. );
  46. }
  47. }
  48. header('Content-Type: application/json; charset=utf-8');
  49. echo json_encode($response, JSON_UNESCAPED_SLASHES);
  50. ?>