forked from MagicMirrorOrg/MagicMirror
/
recipe.php
59 lines (43 loc) · 1.83 KB
/
recipe.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
//libxml_use_internal_errors(true);
$recipe = $_GET['recipe'];
echo get_recipe($recipe);
function get_recipe($recipe) {
global $doc, $xpath;
$url = 'http://www.marmiton.org/recettes/recherche.aspx?aqt='.urlencode($recipe);
$pageList = file_get_contents($url);
// get response list and match recipes titles
if(preg_match_all('#m_titre_resultat[^\<]*<a .*title="(.+)".* href="(.+)"#isU', $pageList, $matchesList)) {
// echo"<xmp>";print_r($matchesList[1]);echo"</xmp>";
// for each recipes titles
// foreach($matchesList[1] as $recipeTitle) {
// }
// take first recipe
$n = 0;
$url = 'http://www.marmiton.org'.$matchesList[2][$n];
$pageRecipe = file_get_contents($url);
// get recipe (minimize/clean before dom load)
if(preg_match('#<div class="m_content_recette_main">.*<div id="recipePrevNext2"></div>\s*</div>#isU', $pageRecipe, $match)) {
$recipe = $match[0];
$recipe = preg_replace('#<script .*</script>#isU', '', $recipe);
$doc = loadDOC($pageRecipe);
$xpath = new DOMXpath($doc);
$recipeTitle = fetchOne('//h1[@class="m_title"]');
$recipeMain = fetchOne('//div[@class="m_content_recette_main"]');
return '<div class="recipe_root">'.$recipeTitle . $recipeMain.'</div>';
}
}
}
function loadDOC($html) {
$doc = new DOMDocument('1.0', 'UTF8');
$doc->formatOutput=false;
@$doc->loadHTML($html, LIBXML_COMPACT | LIBXML_NOERROR | LIBXML_NOBLANKS | LIBXML_NOWARNING | LIBXML_ERR_NONE | LIBXML_NOXMLDECL | LIBXML_HTML_NODEFDTD | LIBXML_PARSEHUGE);
return $doc;
}
function fetchOne($query) {
global $doc, $xpath;
if($res = $xpath->query($query)) {
return $doc->saveHTML($xpath->query($query)->item(0));
}
return false;
}