Всего PHP noob, но нужно что-то делать, грязно и, к сожалению... вчера.
У меня есть удаленная веб-страница, которую я могу извлечь, у нее есть структура узла:
--html
--head
--body
--div
--section id="options"
--section id="video"
--nav id="nav"
// EVERYTHING IN THIS SECTION
--section id="inventory"
--article class="item"
--div class="info"
--div class="inner"
--div class="right"
--span class="price-label"
--span class="price"
--div class="left"
--a href="http://link-to-details-page.html"
--div class="center"
--h2 class="product-title"
--ul class="product-details"
--li
--span title="title1"
--span title="title2"
...............
--div class="clearfix"
--article class="item"
--div class="info"
...............
--div class="clearfix"
...............
...............
// END OF SECTION
--footer
Я кодер, но новичок в php.
Я добрался до этого...
<?php
$url='http://www.example.com/inventory.html';
$homePage=file_get_contents($url);
$dom = new DOMDocument();
@$dom->loadHTML($homePage);
$xpath = new DOMXpath($dom);
$elements = $xpath->query("/html/body");
// would like to filter this more and parse results to json file
//$file = fopen( 'data.json', 'w' );
//fwrite($file, $homePage);
//fclose($fp);
echo htmlspecialchars($homePage);
?>
С моей строкой данных уже в $ homePage, что было бы самым эффективным способом
Огромное спасибо, если вы можете помочь.
ОБНОВИТЬ:
В качестве альтернативы, если вы можете показать мне, как получить только раздел [id = inventory] и передать его функции javaScript, я могу обработать ее оттуда.
РЕШИТЬ
Для кого-то еще борется за это....
getHtml.php
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js">
</script>
</head>
<body>
<div id="dom-target" style="display: none;">
<?php
$url='http://www.example.com/inventory.html';
$content = file_get_contents($url);
$first_step = explode( '<section id="Inventory">' , $content );
$second_step = explode("</section>" , $first_step[1] );
$result = $second_step[0];
$result = trim(preg_replace('/\t+/', '', $result));
echo htmlspecialchars($result);
?>
</div>
<div id="result"></div> <!-- response back is posted here success/fail -->
<script>
$(document).ready(loadContent);
function loadContent() {
var div = document.getElementById("dom-target");
var result = (div.textContent);
// do your code to convert string to json
// i am leaving that part out in the solution
phpSave2File(result);
}
function phpSave2File(result) {
$.ajax({
type: "POST",
url: "/saveJson.php",
data: {data: result},
success: function(res) {
$('#result').html(res);
console.log(res);
},
error: function(err) {
console.log(err.message);
}
});
return false;
}
</script>
</body>
</html>
saveJson.php
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div id="saved-results" style="display: block;">
<?php
$post_data=$_REQUEST["data"];
if( $post_data )
{
$myfile = fopen("currInventory.txt", "w") or die("Unable to open file!");
fwrite($myfile, $post_data);
fclose($myfile);
echo $post_data;
} else {
die('no post data to process');
}
?>
</div>
</body>
</html>