Hello stackoverflow users. I need to pull some movie information from the Open Movie Database API. They have no docs on how to use their API so i am very confused. The site i am creating in PHP needs to pull some variables or strings from their api depending on what imdb ID i put in an form. Here is the code i got so far. This gives me: "http://www.omdbapi.com/?i=tt1092026". But i need to get the strings of their API and make them to variables so i can use them in forms later. How do i do this? Please help. Thanks! :D
<form action="?action=grab" method="post">
<input placeholder="tt1092026" type="text" name="id" id="id">
<input type="submit" class="button" value="Grab Movie">
</form>
<?php if ($_GET[action] == "grab") { ?>
<h9>
<?php
$id = $_POST["id"];
$url = "http://www.omdbapi.com/?i=$id";
echo $url;
?>
</h9>
<?php }; ?>
You need to decode the response and assign it to variables (all in PHP).
<?php
$id = $_POST["id"];
$url = file_get_contents("http://www.omdbapi.com/?i=$id");
$json = json_decode($url, true); //This will convert it to an array
$movie_title = $json['Title'];
$movie_year = $json['Year'];
//and so on...
and then when you need to echo them:
echo $movie_title;
or
echo "The movie '$movie_title' was made in $movie_year.";
I think TMDB is is a better API. http://www.themoviedb.org
They have very good documentation here: http://docs.themoviedb.apiary.io
I'm using this in my own web application and it works like a charm.
Related
I need to show json data from dynamic url in my website. When visitor make input which is his loyalty card number that number is added to url as sufix. That url is url that contains his detals related to loyalty programme (points he earned so far etc.).
My code:
<form action="" method="get">
<input type="text" id="unos" name="card" value = ""><br><br>
<input type="submit" name="submit" value="Provjeri bodove">
</form>
<?php
$card = $_GET["card"];
$url='SERVER_URL_HERE/cardcheck/testAlsLoyalty.php?card= <?php echo $card; ?> ';
$json = file_get_contents($url);
$jo = json_decode($json);
echo $jo->user;
echo $jo->card;
echo $jo->rezultat;
echo $jo->bodova;
echo $jo->raspolozivo;
?>
What I got so far is that this code add input as suffix to url of my website, and if I put url of json website as form action value visitor is taken to json website where he can see data. But I want to load data from that url at my website (html or php) after he type card number.
Thank you in advance
You should check that the variable you're trying to use later on is actually set and also before you append the card number to the url, use rawurlencode() before submitting, otherwise that could break the url (when someone uses unallowed URL characters - i.e. spaces).
<form action="" method="get">
<input type="text" id="unos" name="card" value = ""><br><br>
<input type="submit" name="submit" value="Provjeri bodove">
</form>
<?php
if(isset($_GET["card"])){
$card = $_GET["card"];
$url='http://SERVER_URL_HERE/cardcheck/testAlsLoyalty.php?card=' . rawurlencode($card);
$json = file_get_contents($url);
$jo = json_decode($json);
echo $jo->user;
echo $jo->card;
echo $jo->rezultat;
echo $jo->bodova;
echo $jo->raspolozivo;
}else{
//Notify user here that no card number has been submitted
}
?>
Note that I removed the IP address and port from the URL and replaced with SERVER_URL_HERE. You should never ever publish your public IP address (or domain) as it just opens doors for attackers, especially when you directly give the endpoint...
You don't use <?php echo in strings. Use string concatatenation.
$url='http://81.93.93.78:8085/cardcheck/testAlsLoyalty.php?card=' . $card;
<?php echo ... ?> is used when you've exited PHP code with ?>, and want to insert a PHP expression in the output.
I'm having a problem when using file_get_contents combined with $_GET. For example, I'm trying to load the following page using file_get_contents:
https://bing.com/?q=how+to+tie+a+tie
If I were to load it like this, the page loads fine:
http://localhost/load1.php
<?
echo file_get_contents("https://bing.com/?q=how+to+tie+a+tie");
?>
However, when I load it like this, I'm having problems:
http://localhost/load2.php?url=https://bing.com/?q=how+to+tie+a+tie
<?
$enteredurl = $_GET["url"];
$page = file_get_contents($enteredurl);
echo $page;
?>
When I load using the second method, I get a blank page. Checking the page source returns nothing. When I echo $enteredurl I get "https://bing.com/?q=how to tie a tie". It seems that the "+" signs are gone.
Furthermore, loading http://localhost/load2.php?url=https://bing.com/?q=how works fine. The webpage shows up.
Anyone know what could be causing the problem?
Thanks!
UPDATE
Trying to use urlencode() to achieve this. I have a standard form with input and submit fields:
<form name="search" action="load2.php" method="post">
<input type="text" name="search" />
<input type="submit" value="Go!" />
</form>
Then to update load2.php URL:
<?
$enteredurl = $_GET["url"];
$search = urlencode($_POST["search"]);
if(!empty($search)) {
echo '<script type="text/javascript">window.location="load2.php?url=https://bing.com/?q='.$search.'";</script>';
}
?>
Somewhere here the code is broken. $enteredurl still returns the same value as before. (https://bing.com/?q=how to tie a tie)
You have to encode your parameters properly http://localhost/load2.php?url=https://bing.com/?q=how+to+tie+a+tie should be http://localhost/load2.php?urlhttps%3A%2F%2Fbing.com%2F%3Fq%3Dhow%2Bto%2Btie%2Ba%2Btie. you can use encodeURIComponent in JavaScript to do this or urlencode in php.
<?
$enteredurl = $_GET["url"];
$search = urlencode($_POST["search"]);
if(!empty($search)) {
$url = urlencode('https://bing.com/?q='.$search)
echo '<script type="text/javascript">window.location="load2.php?url='.$url.'";</script>';
}
?>
I have the last url segment of the following url
http://something.com/somefunc/10
that is the number 10;
My real problem is that I have a form that I need to post an action of the url
<form action="../something/somefunc/<?php echo $id>">
//some code
</form>
after receiving the posted stuff, I would like to still be able to get the id of the product under consideration. I think now that form's action url is the only way I know that can help retain such an id. Any help is appreciated, thank you.
[UPDATE]
Uhmmm, all the answers below are correct, don't know which one to choose as the best reply.
Something along these lines may work...
$url = 'http://'. $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$parts = parse_url($url);
$path = $parts['path'];
$keys = explode('/', $path);
echo $keys[4]; // `corresponding to the 4th url word`
Hope this helps!
1.You could pass the product id using POST as a hidden input in your form:
<input name="productid" id="productid" type="hidden" value="<?=$id ?>">
and in the page you post to:
<? if (isset($_POST['productid'])) $id = $_POST['productid'] ?>
2.Alternatively use GET & pass it on the querystring
action="../something/somefunc/[processingpage]?productid=<?=$id ?>"
and in [processingpage]:
<? if (isset($_GET['productid'])) $id = $_GET['productid'] ?>
Sorry if I'm misunderstanding, but can't you get the ID of your posted form via the $_POST array?
http://www.w3schools.com/php/php_post.asp
Alternately, if the ID is in a URL parameter, you can access it via the $_GET variable:
http://www.w3schools.com/php/php_get.asp
You can put a hidden input with the id
<input type="hidden" name="id" value="<?php echo $id; ?>">
How do i update a variable in a php file and save it to disk? I'm trying to make a small CMS, in one file, no database, and i'd like to be able to update the config variables, I've made a settings page and when i update i'd like the variables to updated in the php-file.
Below will update the variable, but of course not save it to the php-file, so how do i do that?
<?php
$USER = "user";
if (isset($_POST["s"]) ) { $USER = $_POST['USER']; }
?>
<html><body>
Current User: <?php echo $USER; ?>
<form method="post" action="<?php echo $_SERVER["SCRIPT_NAME"]; ?>">
New User<input type="text" name="USER" value="<?php echo $USER; ?>"/>
<input type="submit" class="button" name="s" value="Update" />
</form>
</body></html>
I don't know if i'm missing the obvious?
I was thinking of using something like this:
$newcms = file_get_contents(index.php)
str_replace(old_value, new_value, $newcms)
file_puts_contents(index.php, $newcms)
But it doesn't seem like the right solution...
The easiest way is to serialize them to disk and then load them so you might have something like this:
<?php
$configPath = 'path/to/config.file';
$config = array(
'user' => 'user',
);
if(file_exists($configPath)) {
$configData = file_get_contents($configPath);
$config = array_merge($defaults, unserialize($configData));
}
if(isset($_POST['s']) {
// PLEASE SANTIZE USER INPUT
$config['user'] = $_POST['USER'];
// save it to disk
// Youll want to add some error detection messagin if you cant save
file_put_contents($configPath, serialize($config));
}
?>
<html><body>
Current User: <?php echo $config['user'; ?>
<form method="post" action="<?php echo $_SERVER["SCRIPT_NAME"]; ?>">
New User<input type="text" name="USER" value="<?php echo $config['user']; ?>"/>
<input type="submit" class="button" name="s" value="Update" />
</form>
</body></html>
This approach uses PHP's native serialization format which isnt very human readable. If you want to be able to manually update configuration or more easily inspect it you might want to use a different format like JSON, YAML, or XML. JSON will probably be nearly as fast and really easy to work with by using json_encode/json_decode instead of serialize/unserialize. XML will be slower and more cumbersome. YAML is pretty easy to work with too but youll need an external library like sfYaml.
Additionally i wouldnt just do this at the top of a script, id probably make a class of it or a series of functions at the least.
As a better approach, you can have a separate file just for the settings and include that file in the PHP file. Then you can change and save its values as you want instead of modifying the PHP file itself.
for example, you have YOURFILE.php, and inside it you have $targetvariable='hi jenny';
if you want to change that variable, then use this:
<?php
$fl='YOURFILE.php';
/*read operation ->*/ $tmp = fopen($fl, "r"); $content=fread($tmp,filesize($fl)); fclose($tmp);
// here goes your update
$content = preg_replace('/\$targetvariable=\"(.*?)\";/', '$targetvariable="hi Greg";', $content);
/*write operation ->*/ $tmp =fopen($fl, "w"); fwrite($tmp, $content); fclose($tmp);
?>
I have been working on a coding problem for some pages I inherited where the values of two variables are not showing up in the URL of the next page.
The code written by the author, which is probably quite old is
"completeyourorder.php?p=" . $p . "&service=" . $service;
as far as I can tell from my research there should be at least an echo in there
"completeyourorder.php?p=" echo . $p . "&service=" . $service;
or maybe I am missing much more than that.
I'd be grateful for a bit of education on this as I am a newbie and have not been able to find an answer despite many hours of tickering and reading.
Thanks
UPDATE:
PAGE 1
<form action="send-order.php" method="post">
<p>Name<br /><input name="clientname" value="<?echo $clientname;?>" type="text" style="width: 350px;" /></p>
<p><br /><input type="hidden" name="service" value="<?
echo $_GET['service'];
?>" />
<input type="hidden" name="p" value="<?
echo $_GET['p'];
?>"
/>
<input type="submit" value="Order now" /></p>
</form>
PAGE 2
$go = "completeyourorder.php?p=" . $p . "&service=" . $service;
return header("location:$go");
Let me know if you need anything more
SOLVED:
Brilliant thanks it works! I see now that my error was assuming that I could use the variable values in the location: completeyourorder..... on page 2 without having used $_POST earlier in the code on the same page. Thanks to everyone!
What you have shown here is just building part of a string. In php the period is the concatenation operator so it ties two strings together. You need an echo, print, header, or variable assignment on the whole string to make it do something. In that other script then you could get at the variables as
$p = $_REQUEST['p'];
$service = $_REQUEST['service'];
EDIT: upon seeing the update what it is doing is using the header to redirect the script to a new page
You recently asked a similar question regarding $_GET vars.
In this case, it looks like you're building a string to echo out in a link or something. So perhaps this is the what you may be experiencing: (only an example)
<?php
if(isset($_GET['p']))
{
$p = $_GET['p'];
}
if(isset($_GET['service']))
{
$service = $_GET['service'];
}
//defaults in case they are not set
$p or $p = 1; //assuming p = page
$service or $service = 'laundry'; //since I don't know what service refers to
$link = "completeyourorder.php?p=" . $p . "&service=" . $service;
?>
string to click on
Where in this example you can see that you are expecting to build the $link var based on the $_GET vars "p" and "service" and if they do not exist, default to the standard set of page 1 and service of "laundry"
Obviously you would tailor this to fit your needs, but you will need to provide more info for a better, more relevant answer.
EDIT
As per your edit, the basic answer is that your form should be populating the vars for $p and $service, so the revised code would be like this:
<?php
if(isset($_POST['p']) && isset($_POST['service']))
{
$p = $_POST['p'];
$service = $_POST['service'];
// do some kind of post processing here
// possibly do some kind of filtering to ensure the post vars are sanitized
header("location: completeyourorder.php?p=" . $p . "&service=" . $service);
exit;
}
?>
isset is not necessary on both, but you can change that to reflect your preferred choice for verifying post data. Some just do:
if(!empty($_POST))