I try to do this:
$html = file_get_html ( 'http://www.ebay.com/cln/explorer/_ajax?page=1&ipp=16&catids=37958' );
foreach ( $html->find ( 'div[class="connection"]' ) as $collection ) {
echo "found collections: ".count($collection);
Problem is, the returned file from the AJAX request contains elements encoded like:
<div class=\"collection\" data-collectionid=\"75336256016\">
<div class=\"header\">
Can anyone please help me to transform all the \" in the DOM object back to the normal ".
Or change the ->find command to find the right element.
Thanks so much!
$html = file_get_html ( 'http://www.ebay.com/cln/explorer/_ajax?page=1&ipp=16&catids=37958' );
$html = stripslashes($html);
var_dump($html);
string '<div class="collection" data-collectionid="75336256016">
<div class="header">' (length=78)
The response of the ebay is JSON, but there is a <!-- RlogId t6%60jjpfg%3C%3D%60mb6a54d.47e3-143fd4a3ae7-0x32337a --> or something like this at the end of the response.
First you need to clean up that string..
Than json_decode .. value of the html key is your html that you need to parse..
Once you cleaned up and json_decode the response the real html you looked for will be on the object named html.. take a look the code below.. I am pretty sure you can use better var names...
include('simplehtmldom/simple_html_dom.php');
$html = file_get_html ( 'http://www.ebay.com/cln/explorer/_ajax?page=1&ipp=16&catids=37958' );
$str = $html->save();
$strparts = explode('<!-- RlogId',$str);
$json = $strparts[0];
$htmlcleanedup = json_decode($json);
$domhtml = str_get_html($htmlcleanedup->html);
$ret = $domhtml->find('div[class=collection]');
echo count($ret);
?>
Just use stripslashes() function:
http://php.net/stripslashes
Related
I'm experimenting scraping Amazon with PHP but I don't know what I am doing wrong. The problem is that I can't access all the data I scraped. Here is my code:
<?php
$url = 'https://www.amazon.com/s/ref=nb_sb_ss_c_1_9?url=search-alias%3Daps&field-keywords=most+sold+items+on+amazon&sprefix=most+sold%2Caps%2C435&crid=348CE8G406XVG&rh=i%3Aaps%2Ck%3Amost+sold+items+on+amazon';
$html = file_get_html($url);
foreach ($html->find('h2[class=a-size-medium]') as $element) {
echo "<li>" .$element->plaintext."</li><br>";
}
?>
The foreach statement loops through and output the plain text but I want to be able to pass the plain text to a variable or array. The problem is that if I do that and output the result, I only get the last string of the plain text array. I have done lots of research to find what I'm doing wrong but I can't find it. Please any help will be appreciated. Here is what I'm trying to achieve:
<?php
$url = 'https://www.amazon.com/s/ref=nb_sb_ss_c_1_9?url=search-alias%3Daps&field-keywords=most+sold+items+on+amazon&sprefix=most+sold%2Caps%2C435&crid=348CE8G406XVG&rh=i%3Aaps%2Ck%3Amost+sold+items+on+amazon';
$hold = array();
$html = file_get_html($url);
foreach ($html->find('h2[class=a-size-medium]') as $element) {
$hold = $element->plaintext;
}
print_r($hold);
?>
The second code will output the last string of the plain text which is: "Rubbermaid LunchBlox Side Container Kit, 2-Pack, 1806176". I also tried achieving this by encoding and decoding the plain text but nothing changed. What am I doing wrong?
Instead of setting the array hold to a string...add new elements to the array:
$hold[] = $element->plaintext;
I'm trying to deserialize this json.
Actual I stay using the simple html dom library for get the web content, so the next step that I do is using the json_decode() function. But when I'll print the value returned by the function I get NULL. This is the code:
<?php
require_once("simplehtmldom_1_5/simple_html_dom.php");
$html = file_get_html('http://it.soccerway.com/a/block_competition_tables?block_id=page_competition_1_block_competition_tables_8&callback_params=%7B%22season_id%22%3A11663%2C%22round_id%22%3A31554%2C%22outgroup%22%3Afalse%7D&action=changeTable¶ms=%7B%22type%22%3A%22competition_league_table%22%7D');
$decoded = json_decode($html,true);
var_dump($decoded);
?>
What's wrong in my code? Maybe this isn't the best way for doing this? Hint me.
It seems that your file_get_html function is not working properly, you can get the content of a web with file_get_contents
<?php
$html = file_get_contents('http://it.soccerway.com/a/block_competition_tables?block_id=page_competition_1_block_competition_tables_8&callback_params=%7B%22season_id%22%3A11663%2C%22round_id%22%3A31554%2C%22outgroup%22%3Afalse%7D&action=changeTable¶ms=%7B%22type%22%3A%22competition_league_table%22%7D');
$decoded = json_decode($html,true);
var_dump($decoded);
?>
I am retrieving some data from Wikipedia using Wikipedia API as JSON. I am getting html tags along with the data as response. When displaying on browser the html tags are displayed as plain text not as the regular output(I hope I am clear with this sentence). I want to show those as how it should look instead of plain text.following is my PHP code.
$url = 'http://en.wikipedia.org/w/api.php?action=query&prop=extracts&titles=Sachin_Tendulkar&format=json&redirects&inprop=url&indexpageids';
$json = file_get_contents($url);
echo $json ;
Following is the angularJS controller which is used to display the data
var demoApp = angular.module('demoApp',[]);
demoApp.controller('SimpleController',function ($scope,$http){
$http.post('server/view1.php').success(function(data){
$scope.info = data;
});
});
In this case:
$url = 'http://en.wikipedia.org/w/api.php?action=query&prop=extracts&titles=Sachin_Tendulkar&format=json&redirects&inprop=url&indexpageids';
$jsonString = file_get_contents( $url );
$jsonDecoded = json_decode($jsonString, true );
$pageData = $jsonDecoded['query']['pages'][57570]['extract'];
echo $pageData;
You need to use the function json_decode if you want to interpret JSON data.
You must use json_decode($json) to parse response to valid php code.
I am trying to get the contents of a div named: <img id="hplogo-img" src="thelinkiwant"/>
I have this code which isn't working, it just echo's 'Array':
<?php
include_once('simple_html_dom.php');
$html = file_get_html($url);
preg_match('/<img id= \'hplogo-img\'>(.*)<\/div>/s',$html,$matches);
echo $matches;
?>
If it's possible to do this with straight PHP that would be preferred. Any idea's why I can't get the link from the div?
Why not use the method the parser provide.
$ret = $html->find('img[id=hplogo-img]');
$matches is an array.
Try using
print_r($matches)
You should see the arrays content :)
The first element should be what you're looking for. So make:
echo $matches[0];
How to transfer json data to html with php?
$url="http://api.nytimes.com/svc/search/v1/article?format=json&query=usa&rank=newest&api-key=mykey"
when I type the url in browser, it return
{"offset" : "0" , "results" : [{"body" : "A guide to cultural and recreational goings-on in and around the Hudson Valley. ...}]}
how to put the json body data into html? I mean like this echo '<div class="body"></div>';
You first need to get the file. You should use curl for this. In the example below I used the file_get_contents() function, you might want to replace it. Use json_decode() to parse the JSON for you.
$json = file_get_contents($url);
$data = json_decode($json);
echo $data->results[0]->body;
This will echo A guide to cultural....
Use json_decode() on the content of the file, which you can retrieve with file_get_contents($url), then you have an array you can use to build the HTML.
$url="http://api.nytimes.com/svc/search/v1/article?format=json&query=usa&rank=newest&api-key=mykey";
$dataRaw = file_get_contents($url);
if ($dataRaw) {
$data = json_decode($dataRaw, true);
foreach ($data['results'] as $cEntry) {
?>
<div class="body">
<?php echo $cEntry['body']; ?>
</div>
<?php
}
}
I'm not sure why you would, but assuming fopen() URL opening is enabled, you could do...
echo file_get_contents($url);
like this ?
<?php
$url="http://api.nytimes.com/svc/search/v1/article?format=json&query=usa&rank=newest&api-key=mykey";
$json = file_get_contents($url);
echo $json;
Once you've loaded a JSON string into PHP, you can then convert it into a PHP array by using the function json_decode(). You can then print the appropriate array element into your HTML output as with any other PHP variable.
Try this:
$jsonDecoded = json_decode($yourJsonEncodedData);
echo $jsonDecoded->results->body;