I am new to programming and would like to begin adding more advanced applications to my site. I am trying to call an api with php. I'm having trouble getting it to return json format.
<?php
if(!empty($_GET['hospital_name'])) {
$Hospcomp_url = 'https://data.medicare.gov/resource/rbry-mqwu.json?hospital_name=' . urlencode($_GET['hospital_name']);
$Hospcomp_json = file_get_contents($Hospcomp_url);
json_decode($Hospcomp_json, true);
}
?>
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CMS</title>
</head>
<body>
<form action="">
<input type="text" name="hospital_name"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
It seems, you get your result properly but you don't do anything with it. First of all, you don't assign the array decoded from JSON to a variable. And then, you don't echo or process your array otherwise.
Please replace this:
json_decode($Hospcomp_json, true);
with something like this:
$decoded = json_decode($Hospcomp_json, true);
var_export($decoded);
Then you'll have your output and you will be able to decide what to do next.
Calls are correct, just var_export it.
if(!empty($_GET['hospital_name'])) {
$Hospcomp_url = 'https://data.medicare.gov/resource/rbry-mqwu.json?hospital_name=' . urlencode($_GET['hospital_name']);
$Hospcomp_json = file_get_contents($Hospcomp_url);
var_export(json_decode($Hospcomp_json, true));
}
Related
I have a php form that users submit their start / end dates. The form then needs to pull the results from .xml hosted on a different URL. The URL varies based on the dates entered by the user - dates become part of the URL as you can see below.
So, simply put... The user enters the dates. It calls the xml file via the URL (which changes slightly as the dates are entered into that URL) and it displays the results on a new page.
I would rather do it via PHP than AJAX if possible.
This is the frontend of my form:
<html>
<body>
<form action="test_get.php" method="get">
Start Date: <input type="text" id="start" name="start"><br>
End Date: <input type="text" id="end" name="end"> <br>
<input type="submit">
</form>
</body>
</html>
This is 'test_get.php':
<html>
<body>
<form onSubmit="return process();">
Start Date: <?php echo $_GET["start"]; ?><br>
End Date: <?php echo $_GET["end"]; ?>
</form>
</body>
<script>
function process()
{
var url="http://99.999.999.999:81/fmi/xml/fmresultset.xml?-db=Front_Desk&-lay=WebRoomQuery&-findany&-script=WebQueryPSOS&-script.param=snug," + document.getElementById("start").value + "," + document.getElementById("end").value;
location.href=url;
return false;
}
</script>
</html>
But it doesn't work. I also tried this:
<html>
<body>
<?php
$xml=simplexml_load_file("http://99.999.999.999:81/fmi/xml/fmresultset.xml?-db=Front_Desk&-lay=WebRoomQuery&-findany&-script=WebQueryPSOS&-script.param=snug," + document.getElementById("start").value + "," + document.getElementById("end").value"); or die("Error: Cannot create object");
echo $xml->field . "<br>";
echo $xml->data . "<br>";
?>
</body>
</html>
But again, no luck. Can anyone see what i'm missing?
I can also share what my .xml form looks like if needed. Any help is greatly appreciated - i've been tackling this for days! Thanks! :)
UPDATE: This is my updated code for test_get.php:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<?php
$url = '"http://69.239.118.197:81/fmi/xml/fmresultset.xml?-db=Front_Desk&-lay=WebRoomQuery&-findany&-script=WebQueryPSOS&-script.param=snug,'.$_GET['start'].','.$_GET['end'].'"';
$xml = simplexml_load_file($url);
echo $xml->field . "<br>";
echo $xml->data . "<br>";
echo var_dump($xml). "<br />";
?>
</body>
</html>
In the last block of code that you posted, you are using JavaScript outside of script tags, so this is definitely not going to work. Have you tried instead putting your $_GET variable into the query string?
Note* I pulled out the query string for more readability. It's hard for me to verify that this is working for you outside of your environment, but give it a shot.
You've got this:
$xml=simplexml_load_file("http://99.999.999.999:81/fmi/xml/fmresultset.xml?-db=Front_Desk&-lay=WebRoomQuery&-findany&-script=WebQueryPSOS&-script.param=snug," + document.getElementById("start").value + "," + document.getElementById("end").value"); or die("Error: Cannot create object");
I'm recommending this:
$url = '"http://99.999.999.999:81/fmi/xml/fmresultset.xml?-db=Front_Desk&-lay=WebRoomQuery&-findany&-script=WebQueryPSOS&-script.param=snug,'.$_GET['start'].','.$_GET['end'].'"';
$xml = simplexml_load_file($url);
*************EDIT*****************
So, after attempting to manipulate the data that simplexml_load_file returns I decided to try file_get_contents. The solution below returns the data value from the xml file in string format.
$_GET['start'] = '08/30/2017';
$_GET['end'] = '08/31/2017';
$xml = file_get_contents("http://69.239.118.197:81/fmi/xml/fmresultset.xml?-db=Front_Desk&-lay=WebRoomQuery&-findany&-script=WebQueryPSOS&-script.param=snug,".$_GET['start'].','.$_GET['end'].'"');
echo $xml;
I have to write json result in a string.
Here is my code,
<!DOCTYPE html> <html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form method="POST">
Enter Pin <input type="text" name="pinCode">
<input type="submit" name="formSubmit">
</form>
</body> </html>
<?php
if(isset($_POST['formSubmit']))
{
$input = $_POST['pinCode'];
$shortUrl=file_get_contents("https://www.whizapi.com/api/v2/util/uiin/indian-city-by-postal-code?project-app-key=<app_key>&pin=".$input);
$res = json_decode($shortUrl, true);
echo implode($res);
}
?>
Currently the outcome is on json format. I have to print the result in string. e.g - { "title" : "Mr", "name" : "sandeep"}. Result would be like "Mr sandeep". That's why I have used json_decode for changing json into array but then I couldn't understand how to change associative array in string.
Thanks in advance.
Don't decode the json if you want it in string. The output is string and you are converting it to array using json_decode so just comment that line
if(isset($_POST['formSubmit']))
{
$input = $_POST['pinCode'];
$shortUrl=file_get_contents("https://www.whizapi.com/api/v2/util/ui/in/indian-city-by-postal-code?project-app-key=<app_key>&pin=".$input);
//$res = json_decode($shortUrl, true);
echo $shortUrl;
}
I am a junior programmer. Recently I've been trying to develop in PHP. I am trying to create a basic page about countries. For some reason I cannot grab the information from this API page. Can anyone give me some tips?
Here is what a sample country array looks like. Ideally I would like to try to grab the "name" attribute.
Here is my HTML and PHP:
<?php
if(!empty($_GET['country'])) {
$country_url = 'https://restcountries.eu/rest/v1/name/' . urlencode($_GET['country']) . '?fullText=true';
$country_json = file_get_contents($country_url);
$country_array = json_decode($country_url, true);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset = "utf-8" />
<title> PHP Country Practice </title>
</head>
<body>
<form action="">
<input type = "text" name = "country" />
<button type="submit">submit </button>
</form>
<?php
if(!empty($country_array)) {
echo '<p> $country_array['name'] </p>';
}
?>
</body>
</html>
You're returning an associative array, so use identifiers like this:
$country_array[0]['name']
If it is object use the below code.
echo $country_array[0]->name;
For array use
echo $country_array[0]['name'];
You need to access the correct array index, in this case 0.
Variables don't expand inside single-quotes, you need to use
double-quotes and surround it with brackets {} (tks AbraCadaver), i.e.:
echo "<p> {$country_array[0]['name']} </p>";
<?php
if(isset($_GET['textvalue'])){
$string = $_GET['textvalue']; //preg_match return false
//$string = '한자漢字メ'; //preg_match return true
$stringArray = preg_match('/^[\p{L}]{2,30}$/u', $string);
}
?>
<!DOCTYPE html>
<html>
<body>
<form method="GET">
<input type="text" name="textvalue">
<input type="submit">
</form>
</body>
</html>
I'm trying to regex the value from the input.
Unfortunately, every time I submit the characters, preg_match return false. But, if I use the string from the variable, it'll return true.
What going on and how do I fix it?
If anyone ran into this problem, I've found it. You just need to add this meta header:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
I'm not sure why, but with out the codes above, html it send the values to php as a non-utf-8 value. So, then the preg_match try to read it, its reading a different value then what was typed in, thus; it return false.
That's why it work when you just uses the string. HTml is not involved.
note. Even if you try to read by echoing it out, html with return it to its orginal utf-8 value. weird.
Example:
<?php
if(isset($_GET['textvalue'])){
$string = $_GET['textvalue']; //preg_match return false
//$string = '한자漢字メ'; //preg_match return true
$stringArray = preg_match('/^[\p{L}]{2,30}$/u', $string);
}
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
<body>
<form method="GET">
<input type="text" name="textvalue">
<input type="submit">
</form>
</body>
</html>
I'm really new to PHP, and I'm trying to use the simple captcha mentioned in this question
Numeric Captcha for PHP
What I'm trying to do is pass the $_SESSION['captcha'] to my current page, so it can compare with the input I just entered which is supposed to pass by the "form".
Here's my code:
<?php
if(isset($_POST['captcha'] ,$_SESSION['captcha'])) {
if ($_POST['captcha'] == $_SESSION['captcha'])
echo 'YES, YOU DID IT';
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>captcha test</title>
</head>
<body>
<form name="input" action="index.php" method="POST">
<img src="captcha.php">
Enter the code above: <input type="text" name="captcha">
<input type="submit" value="Submit">
</form>
</body>
</html>
What's the correct way to do this? How can I implement the captcha in my current code?
A few things fixed here:
First you did not use a session_start() to actually retrieve $_SESSION values. Added here.
Next, you should check if the values are not empty by using !empty() in addition to isset(). Added here as well.
Finally, I recommend you use === comparison operator instead of ==. While == will check if values are the same, === will check if they are the same and the same data type.
Here is the cleaned up code:
<?php
session_start();
if (isset($_POST['captcha'], $_SESSION['captcha']) &&
!empty($_POST['captcha']) && !empty($_SESSION['captcha'])) {
if ($_POST['captcha'] === $_SESSION['captcha']) {
echo 'YES, YOU DID IT';
}
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>captcha test</title>
</head>
<body>
<form name="input" action="index.php" method="POST">
<img src="captcha.php">
Enter the code above: <input type="text" name="captcha">
<input type="submit" value="Submit">
</form>
</body>
</html>
If this still does not work for you, you can dump the values of $_POST and $_SESSION like this to see what you are getting:
echo '<pre>';
print_r($_POST);
echo '</pre>';
echo '<pre>';
print_r($_SESSION);
echo '</pre>';
You didn't add session_start() at the 1st line, thus the $_SESSION is empty.
Side Note:
You mixed up XHTML and HTML. xmlns is not required for HTML.
DOCTYPE is missing
You need to start your session with
session_start();
as a first call before your script has any output.
See documentation: http://php.net/manual/en/function.session-start.php