I am making a home automantion project with Arduino and I am using Teleduino to remotely control an LED as a test. I want to take the contents of this link and display them into a php page.
<!DOCTYPE html>
<html>
<body>
<?php
include 'simple_html_dom.php';
echo file_get_html('http://us01.proxy.teleduino.org/api/1.0/2560.php?k=202A57E66167ADBDC55A931D3144BE37&r=definePinMode&pin=7&mode=1');
?>
</body>
The problem is that the function does not return anything.
Is something wrong with my code?
Is there any other function I can use to send a request to a page and get that page in return?
I think you had to use function file_get_contents but your server is protcting data from scraping so curl would be a better solution:
<?php
// echo file_get_contents('http://us01.proxy.teleduino.org/api/1.0/2560php?k=202A57E66167ADBDC55A931D3144BE37&r=definePinMode&pin=7&mode=1');
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "http://us01.proxy.teleduino.org/api/1.0/2560.php?k=202A57E66167ADBDC55A931D3144BE37&r=definePinMode&pin=7&mode=1");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
// $output contains the output string
$output = curl_exec($ch);
echo $output;
// close curl resource to free up system resources
curl_close($ch);
?>
Related
What i'm trying to do is do a search on Amazon using a random keyword, then i'll just scrape maybe the first 10 results, the issue when i print the html results i get nothing, it's just blank, my code looks ok to me and i have used CURL in the past and never come accross this, my code:
<?php
include_once("classes/simple_html_dom.php");
function get_random_keyword() {
$f_contents = file("keywords.txt");
return $f_contents[rand(0, count($f_contents) - 1)];
}
function getHtml($page) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $page);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5');
$html = curl_exec($ch);
print "html -> " . $html;
curl_close($ch);
return $html;
}
$html = getHtml("https://www.amazon.co.uk/s?k=" . get_random_keyword());
?>
Ideally i would have preferred to use the API, but from what i understand you need 3 sales first before you are granted access, can anyone see any issues? i'm not sure what else to check, any help is appreciated.
Amazon is returning the response encoded in gzip. You need to decode it:
$html = getHtml("https://www.amazon.co.uk/s?k=" . get_random_keyword());
echo gzdecode($html);
I have been able to parse actual .json files, but this link I can't seem to parse.
http://forecast.weather.gov/MapClick.php?lat=36.321903791028205&lon=-96.80576767853478&FcstType=json
I am thinking because the link itself is not a .json file but a json formatted link... and I am having issues trying to parse it... even if I start by using...
<?php
$url = "http://forecast.weather.gov/MapClick.php?lat=36.321903791028205&lon=-96.80576767853478&FcstType=json";
$json = file_get_contents($url);
$json_a = json_decode($json,true);
// <---------- Current Conditions ----------> //
//Display Location
$location_full = $json_a['location']['areaDescription'];
?>
And the on my page I want to display this information I have:
<?php
require 'req/weatherinfo.php';
?>
<!DOCTYPE html>
<html>
<head>
<title>PawneeTV Weather</title>
</head>
<body>
<?php echo $location_full; ?><p>
</body>
</html>
Any ideas why its generating a blank page? I have cleared the errors now it just doesn't display anything. I've done with many times with a .json file source, it works with this source http://api.wunderground.com/api/43279e1c0b065c2e/forecast/q/OK/Pawnee.json, but will not work with a link thats ends with =json instead of .json
You can not use file_get_contents in that case. More explanation about this you can read here.
This code is working:
<?php
$url = "http://forecast.weather.gov/MapClick.php?lat=36.321903791028205&lon=-96.80576767853478&FcstType=json";
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, $url);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
$json_a = json_decode($output,true);
// <---------- Current Conditions ----------> //
//Display Location
$location_full = $json_a['location']['areaDescription'];
Im using php, curl, and simple_dom_document to get snow data from snowbird.com. The problem is I cant seem to actually find the data I need. I am able to find the parent div and its name but I cant find the actually snow info div. Here is my code. Below my code ill past a small part of the output.
<?php
require('simple_html_dom.php');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.snowbird.com/mountain-report/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
$content = curl_exec($ch);
curl_close($ch);
$html = new simple_html_dom();
$html->load($content);
$ret = $html->find('.horizSnowChartText');
$ret = serialize($ret);
$ret3 = new simple_html_dom();
$ret3->load($ret);
$es = $ret3->find('text');
$ret2 = $ret3->find('.total-inches');
print_r($ret2);
//print_r($es);
?>
And here is a picture of the output. You can see it skips the actual snow data and goes right to the inches mark ".
Do note that the html markup you're getting has multiple instances of .total-inches (multiple nodes with this class). If you want to explicitly get one, you can point to it directly using the second argument of ->find().
Example:
$ret2 = $html->find('.total-inches', 3);
// ^
If you want to check them all out, a simple foreach should suffice:
foreach($html->find('.current-conditions .snowfall-total .total-inches') as $in) {
echo $in , "\n";
}
I have to put this page: http://www.tvindiretta.com/m/ in a iframe. This page is cURL powered. He is it's content. When I try to put this url: http://www.tvindiretta.com/m/index.php in an iframe (with tag) the browser redirects to the iframe url. How can I keep this page inside the iframe. I have to change the user user agent. the I'm a complete noob in cURL but help me please. He is the /m/index.php page source code:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.tvindiretta.com/");
curl_setopt($ch, CURLOPT_MAXREDIRS, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent: Mozilla/5.0 (iPhone; U; CPU iPhone OS 2_2_1 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5H11 Safari/525.20'));
curl_exec($ch);
$result = curl_exec ($ch);
curl_close ($ch);
print $result;
curl_close($ch);
?> $
I don't think there is an user-agent redirection on this web page since
<?php
if (isset($_GET['get'])){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.tvindiretta.com/m");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_exec($ch);
$result = curl_exec ($ch);
curl_close ($ch);
print $result;
}
else{
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<iframe src="test.php?get" style="position:absolute; top:100px; left:100px; width:400px; height:400px;"/>
</body>
</html>
<?php } ?>
Seems to screw the page, but provide me the mobile content anyway.
So I guess the real problem here is the javascript code inside that page:
In html5 you have a new iframe attribute "sandbox" which allows you to restrict the iframe's content behaviour .
Unfortunately this seems to be supported only by Chrome and Safari.
One idea here could be to try to scrape the content of the web page (with DomDocument in PHP for instance), keep only the content in which you are interested, and try to reproduce their style. It may be easier to say than to do, but I can't see a cleaner way to do so.
Since it seems you are interested in getting a TV program, you could check for a dedicated xml scaper XMLtv.
header('Content-Type: image/jpeg');
$imageURL = $_POST['url'];
$image = #ImageCreateFromString(#file_get_contents($imageURL));
if (is_resource($image) === true)
imagejpeg($image, 'NameYouWantGoesHere.jpg');
else
echo "This image ain't quite cuttin it.";
This is the code I have to convert a url that I receive from an html form into an image. However, whenever I try to display it or take it off the server to look at it, it 'cannot be read' or is 'corrupted'. So for some reason it is converted to an image, recognized as a proper resource, but is not proper image at that point. Any ideas?
You don't want ImageCreateFromString - using file_get_contents is getting you the actual binary data for the image.
Try $image = #imagecreatefromjpeg(#file_get_contents($imageURL)); and see if you like the results better (assuming the original is a JPEG).
You can use cURL to open remote file:
$ch = curl_init();
// set the url to fetch
curl_setopt($ch, CURLOPT_URL, 'http://www.google.com/img.jpg');
// don't give me the headers just the content
curl_setopt($ch, CURLOPT_HEADER, 0);
// return the value instead of printing the response to browser
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// use a user agent to mimic a browser
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0');
$content = curl_exec($ch);
// remember to always close the session and free all resources
curl_close($ch);