php xml url's with multiple words - php

having an annoying issue with getting xml data from a url
if i set "$file_show_title" to a single word (comes from the url passed from another page) then all works fine. if if "$file_show_title" is 2 words (tried encoding the url also) then it failed to find the address.
im not the best at explaining but i hope that this is enough for someone to help me please
thanks
$url = file_get_contents("http://www.thetvdb.com/api/GetSeries.php?seriesname=".$file_show_title);
$xml = simplexml_load_string($url);
$seriesid = $xml->Series[0]->seriesid;
$seriesbanner = $xml->Series[0]->banner;
$seriesname = $xml->Series[0]->SeriesName;
$serieslanguage = $xml->Series[0]->language;
$seriesfist_aired = $xml->Series[0]->FirstAired;
$seriesoverview = $xml->Series[0]->Overview;
echo '<img src="/images/'.$seriesbanner.'">'."<br />";
echo $seriesname."<br />";
echo $seriesid."<br />";
echo $serieslanguage."<br />";
echo $seriesfist_aired."<br />";
echo $seriesoverview."<br />";

What you needed was urlencode Please see http://php.net/manual/en/function.urlencode.php for more information
Example
$url = file_get_contents("http://www.thetvdb.com/api/GetSeries.php?seriesname=" . urlencode("Prison Break"));
Now Use
$url = file_get_contents("http://www.thetvdb.com/api/GetSeries.php?seriesname=" . urlencode($file_show_title));
I hope it helps

Related

how to get a third variable using a doubled join variable in php

PHP CODE:
$video1="welcome.mp4";
$video2="movie.mp4";
$video3="ends.mp4";
$num_id="1";
$get = '$' . "video" . $num_id;
$file = $get;
echo "<a href=Player.php?file=$file'>Play</a>";
html code results:
Play
HTML - expectancy:
Play
I would do something like this.
$videos = ['welcome.mp4','movie.mp4','ends.mp4'];
foreach($videos as $video){
echo "<a href=Player.php?file=$video'>Play</a><br>";
}
I would do this (If I wanted to be Kool, or confuse a junior developer .. lol )
$video1="welcome.mp4";
$video2="movie.mp4";
$video3="ends.mp4";
$num_id="1";
$get = ${"video".$num_id};
$file = $get;
echo "<a href=Player.php?file=$file'>Play</a>";
Output
<a href=Player.php?file=welcome.mp4'>Play</a>
But I am not sure you are ready to have access to that much power...
Sandbox

PHP not getting right link from path

Ok, so I have literally never been so confused. As you can see I have pretty much the same function twice here (I know that may seem stupid but it is just easier for me to read for my page when it's like this - but that isn't the point of this)
The first one goes to the link it's given (http://www.blade-edge.com/images/KSA/Flights/craft.asp?db=dunai) then follows the path to get the img src of http://i.imgur.com/8t5rwWh.png
But the second function doesn't get the src of the image it's pointing to (which would be http://i.imgur.com/jWWUEqt.png) but instead gets the src for a completely different image on the page http://www.blade-edge.com/images/KSA/Flights/prev.png.
I am sure this is a really stupid mistake that I have just overlooked but I can't work it out.
Anyone?
function getImage(){
$page = file_get_html(getPageURL());
$element = $page->find("html/body/div/div[1]/center/table/tbody/tr[1]/td/table/tbody/tr/td[1]/div/div/img");
$imgLink = $element[0]->getAttribute("src");
echo "<img id='shipImage' src='".$imgLink."'></img>";
}
function getMap(){
$page = file_get_html(getPageURL());
$element = $page->find("/html/body/div/div[1]/center/table/tbody/tr[2]/td/center/img");
$imgLink = $element[0]->getAttribute("src");
echo "<img id='shipMap' src='".$imgLink."'></img>";
}
The following works for me:
function getImage($imageType){
$page = file_get_html(getPageURL());
$element = $page->find("/html/body/div/div[1]/center/table/tbody", 0)->children($imageType)->find("img");
$imgLink = $element[0]->getAttribute("src");
return $imgLink;
}
echo "<img id='shipImage' src='" . getImage(0) . "'></img>"; // Spacecraft image
echo "<img id='shipMap' src='" . getImage(1) . "'></img>"; // Map image
I won't try to guess the reason behind the problem, as I do not know the innards of the library.

cache googlemaps geocoding results but my code is broken

I posted this question last night but I worded it wrong and didn't explain correctly. I am trying to cache googlemaps geocoding results for use in a firefighting mapping thing I have made and I am getting close to google's limits hence the need to cache the results. Help!
The code below kinda works however it creates a new sql record each time the code runs regardless of whether the address is already in the database. It seems to only call google once then saves the data, it then loads the data from the database ok and it looks like the rest works but I just can't see where I am going wrong.. As I said it creates a new record each and everytime the code runs and I would end up with a database full of the same identical records. My brain hurts but I really need to get this working so I can continue to help fire trucks to fires. :)
Oh, I am using the geocoding results on googlemaps (as permitted by the T&C) and this code is really only to get it working. I hope that makes sense and I hope someone can help... Thanks :)
<?php
$jobadd = "1000 BURWOOD HWY, BURWOOD";
// connect to the database
include('connect-db.php');
// get results from database and find needle
$result = mysql_query("SELECT * FROM geocache")
or die(mysql_error());
while($row = mysql_fetch_array( $result ))
{
$needle = '' . $row['address'] . '';
if (strpos($jobadd,$needle) !== false)
{
$status = "CACHED";
$latitude = '' . $row['latitude'] . '';
$longitude = '' . $row['longitude'] . '';
}
else
{
$status = "GOOGLE";
$address2 = "$jobadd, Victoria, Australia";
define("MAPS_HOST", "maps.google.com");
$base_url = "http://" . MAPS_HOST . "/maps/api/geocode/xml";
$request_url = $base_url . "?address=" . urlencode($address2) ."&sensor=false";
$xml = new SimpleXMLElement(file_get_contents($request_url));
$latitude = $xml->result->geometry->location->lat;
$longitude = $xml->result->geometry->location->lng;
// save the data to the database
mysql_query("INSERT geocache SET address='$jobadd', latitude='$latitude', longitude='$longitude' ")
or die(mysql_error());
}
}
echo $status;
echo '<BR>';
echo $jobadd;
echo '<BR>';
echo 'LAT:';
echo $latitude;
echo '<BR>';
echo 'LON:';
echo $longitude;
?>
Have you tried to output the result of strpos? The logic does not look incorrect. The issue is in the if condition. What do the records look like in the DB?

how to use urlencode( ) in my example?

I checked php.net and read a few examples of how urlencode( ) works but somehow I just can't get it right. Can someone give me a hand?
it'll be a lot to example so hopefully my brief example would make sense.
I have a page called 2.php and it was called to show some contents of a .txt file choosen in 1.php.
I am told to make a link for 3.php and the link should look something like /3?filename=a.txt
with filename as GET parameter name and Ensure GET parameter value is urlencoded using the urlencode( ) function.
but I'm confused how and where I should put urlencode() to make it work.
I'll paste my 2.php code here...I simplified the codes a bit...
<?php
$fileContents = file("./aaa/" . $_GET["course"] . ".txt");
echo "<table border=\"1\">";
foreach($fileContents as $row)
{
echo "<tr>";
$contents = preg_split("/,/", $row);
foreach($contents as $eachline)
{
echo "<td>";
if(!(preg_match("/#/", $eachline)))
{
echo trim(ucfirst($eachline));
}
else
{
echo trim(strtolower($eachline));
}
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
echo "<a href='./1.php'>Choose another txt file</a><br/>";
echo "or<br/>";
echo "<a href='.3.php?'>Work with this txt file</a>";
?>
BUT…the 3.php option must have a query string appended to it: the name of the text file that was selected in 1, so instead of ./3.php, the url should be something such as ./3?filename=asdf.txt
Use “filename” as the GET parameter name. Ensure the GET parameter value is urlencoded using the urlencode( ) function.
but I'm just not sure how to get it to work....
You can wrap the part that should be url encoded in the function within the string:
$url = 'http://www.google.com?q=' . urlencode($search);
OR in html
http://www.google.com?q=<?php echo urlencode($search); ?>
Where . is the concatenation of 2 outputs.

Is there something wrong with this XML/PHP Code for Tumblr to Website?

I am trying to link a tumblr feed to a website. I found this code (As you can see, something must be broken with it as it doesnt even format correctly in this post):
<?php
$request_url = “http://thewalkingtree.tumblr.com/api/read?type=post&start=0&num=1”;
$xml = simplexml_load_file($request_url);
$title = $xml->posts->post->{‘regular-title’};
$post = $xml->posts->post->{‘regular-body’};
$link = $xml->posts->post[‘url’];
$small_post = substr($post,0,320);
echo ‘<h1>’.$title.’</h1>’;
echo ‘<p>’.$small_post.’</p>’;
echo “…”;
echo “</br><a target=frame2 href=’”.$link.”’>Read More</a>”;
?>
And i inserted the tumblr link that I will be using. When I try to preview my HTML, i get a bunch of messed up code that reads as follows:
posts->post->{'regular-title'}; $post = $xml->posts->post->{'regular-body'}; $link = $xml->posts->post['url']; $small_post = substr($post,0,320); echo '
'.$title.'
'; echo '
'.$small_post.'
'; echo "…"; echo "Read More"; ?>
Any help would be appreciated. Thank you!
That is PHP, not HTML. You need to process it with a PHP parser before delivering it to a web browser.
… it should also be rewritten so it can cache the remote data, and escape special characters before injecting the data into an HTML document.

Categories