I Want to Parse Show Time of Some city from Google Page:
Here I Get Movie Names from Page1:
http://www.google.co.in/movies?near=chennai&hl=en&ei=8OIaUr-0EYrFkwX9kICQCA_&date=0
And I Get Show timings from Page2:
http://www.google.co.in/movies?near=chennai&hl=en&ei=8OIaUr-0EYrFkwX9kICQCA_&date=0&tid=8a030c04960c6341
My Php Code:
$htm = file_get_html('http://www.google.co.in/movies?near=chennai&hl=en&ei=8OIaUr-0EYrFkwX9kICQCA_&date=0');
$linker = $htm->find('div h2 a');
$value = $linker[2]->href;
$link = "http://www.google.co.in$value";
$htmls = file_get_html($link);
If I echo "$link"; it Print Same link as Page 2:
$cinemaname = $htmls->find('div[class=name]');
$cinematimes = $htmls->find('div[class=times]');
echo strip_tags($cinemaname[0])."<div>";
echo strip_tags($cinematimes[0])."<div>";
If I echo $cinemaname[0]"; It Printing from Page 1, but I want to Print from Page2.
This issue about link encoded. Your link was return below: &
http://www.google.co.in/movies?near=chennai&hl=en&ei=8OIaUr-0EYrFkwX9kICQCA_&date=0&tid=8a030c04960c6341
You have to decode it with html_entity_decode function
$value = $linker[2]->href;
$link = html_entity_decode("http://www.google.co.in".$value);
$htmls = file_get_html($link);
Related
when i type in a movie name in the search field i want to display all the movies that is the json file but with this code i can only get one of the movies can you please help me with this.
<?php
if (isset($_POST['submit-search'])) {
$txtresult = $_POST['search'];
function getImdbRecord($title, $ApiKey)
{
$path = "http://www.omdbapi.com/?s=$title&apikey=$ApiKey";
$json = file_get_contents($path);
return json_decode($json, TRUE);
}
$data = getImdbRecord($txtresult, "f3d054e8");
echo "<div class = 'info-box'><img src =".$data['Poster']."</img><h3> Name :".$data['Title']."</h3><h3> Year : ".$data['Year']."</h3><h3> Duration : ".$data['Runtime'],"</h3></div>";
}
?>
you have need to use foreach loop to get all the search result.
like.
$data = getImdbRecord($txtresult, "f3d054e8");
foreach($data['Search'] as $value){
echo "<div class = 'info-box'><img src =".$value['Poster']."</img><h3> Name :".$value['Title']."</h3><h3> Year : ".$value['Year']."</h3><h3> Duration : ".$value['Runtime'],"</h3></div>";
}
Complete code will be.
<?php
//add function out side the if condition
function getImdbRecord($title, $ApiKey){
$path = "http://www.omdbapi.com/?s=$title&apikey=$ApiKey";
$json = file_get_contents($path);
return json_decode($json, TRUE);
}
if (isset($_POST['submit-search'])) {
$txtresult = $_POST['search'];
$data = getImdbRecord($txtresult, "f3d054e8");
//use loop to get all the seacrh result.
foreach($data['Search'] as $value){
echo "<div class = 'info-box'><img src =".$value['Poster']."</img><h3> Name :".$value['Title']."</h3><h3> Year : ".$value['Year']."</h3><h3> Duration : ".$value['Runtime'],"</h3></div>";
}
}
?>
I would like to change all links in an HTML variable to random ones. Here is my code but something prevents links from being changed:
<?php
$jobTemplateDetails = 'Click!
Click!';
////////////////////// CHANGE ALL LINKS
$linkDom = new DOMDocument;
#$linkDom->loadHTML($jobTemplateDetails);
$allLinks = $linkDom->getElementsByTagName('a');
foreach ($allLinks as $rawLink) {
$longLink = $rawLink->getAttribute('href');
$str = 'abcdefghijklmnopqrstuvwxyz';
$randomChar1 = $str[mt_rand(0, strlen($str)-1)];
$randomChar2 = $str[mt_rand(0, strlen($str)-1)];
$randomChar3 = $str[mt_rand(0, strlen($str)-1)];
$randomChar4 = $str[mt_rand(0, strlen($str)-1)];
$shortURL = mt_rand(1, 9).$randomChar1.mt_rand(1, 9).$randomChar2.$randomChar3.$randomChar4;
$rawLink->setAttribute('href', $shortURL);
}
echo $jobTemplateDetails;
When you echo $jobTemplateDetails; you only show the very first input string, not the DomDocument you manipulate.
Change that to
echo $linkDom->saveHTML();
///OUTPUT:
Click!
Click!
a fiddle: https://3v4l.org/KuCic
and the docs
I need help in php:
Imagine visiting the link "http://example.com/gen" and every time I visit the result in html on the page is different:
code=0001
refresh the page ...
code=1545
refresh the page ...
code=7845
....
I want to know how can I get the following code to the = sign to use in most places of the page
Makes an array of 10 codes from the URL:
<?php
$codes = array();
for($i = 1; $i<=10; $i++){
$string = file_get_contents("URLHERE");
$string = explode('=',$string);
$code = $string[1];
$codes[] = $code;
}
?>
Usage Usage:
foreach($codes as $code)
echo $code . "<br />";
echo "First Code: {codes[0]}";
?>
I am grabbing the comments from videos, but the "author name" and their id is giving NULL.
This is what I have.
$feedUrl='http://gdata.youtube.com/feeds/api/videos/'.$selected_video_id.'/comments?v=2&alt=json';
$data = json_decode(file_get_contents($feedUrl),true);
$info = $data["feed"];
$entry = $info["entry"];
$nEntry = count($entry);
for($i=0;$i<$nEntry;$i++){
$name = $entry[$i]['author']['name']['$t'];
$userId = $entry[$i]['author']['yt$userId']['$t'];
$content = $entry[$i]['content']['$t'];
$published_2 = $entry[$i]['published']['$t'];
}
Content and Published are collected fine, but the name and userID are not.
The feed does have the elements in it as have looked at it in the youtube data api demo beta. As well as it shows everything if you do a feed request in the browser.
http://gdata.youtube.com/feeds/api/videos/VIDEO_ID/comments
So am I missing something?
It should be:
$name = $entry[$i]['author'][0]['name']['$t'];
$userId = $entry[$i]['author'][0]['yt$userId']['$t'];
$content = $entry[$i]['content']['$t'];
$published_2 = $entry[$i]['published']['$t'];
Or better yet:
$feedUrl=file_get_contents('http://gdata.youtube.com/feeds/api/videos/ASO_zypdnsQ/comments?v=2&alt=json');
$json = json_decode($feedUrl, true);
foreach($json['feed']['entry'] as $entry) {
echo $entry['author'][0]['name']['$t']."<br>";
echo $entry['author'][0]['yt$userId']['$t']."<br>";
echo $entry['content']['$t']."<br>";
echo $entry['published']['$t']."<br>";
}
You can use foreach like the above :)
I am trying to change languages but stay on the current page:
i.e:
www.myurl.com/english/aboutus.php
www.myurl.com/german/aboutus.php
www.myurl.com/french/aboutus.php
So only the language directory changes.
I have the following but can't get it to work:
<?php
$full_url = $_SERVER['FULL_URL'] ;
$temparray = explode(".com/",$full_url,2);
$englishtemp = $temparray[0] . ".com/english/" . $temparray[1];
$englishlink = "<a href=$englishtemp><img src=../images/english-flag.jpg></a>";
echo $englishlink;
?>
I get the url to change from '/french/aboutus.php' to '/english' but it doesn't remember the page name 'aboutus.php' and returns to the index page.
Could any one please help?
Use basename($_SERVER['PHP_SELF']) for the current page.
$englishtemp = "/english/" . basename($_SERVER['PHP_SELF']);
$englishlink = "<a href=$englishtemp><img src=../images/english-flag.jpg></a>";
echo $englishlink;
See PHP documentation on $_SERVER.
Try this instead. It will generate links for all the languages except the current selected one :)
<?php
$full_url = $_SERVER['REQUEST_URI']; //select full url without the domain name
$languages = array('english', 'french', 'german'); //array of all supported languages
$url_bits = explode('/', $full_url); //divide url into bits by /
$current_language = $url_bits[1]; //current language is held in the second item of the array
//find current language in the array and remove it so we wouldn't have to display it
unset($languages[array_search($current_language, $languages)]);
$links = '';
foreach ($languages as $language) {
//generate links with images for the rest of the languages
$links .= '<img src="../images/' . $language . '-flag.jpg" title="'.ucfirst($language).'" />';
}
echo $links;
?>