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;
}
Related
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));
}
This is my html page:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Hello and welcome to my site</title>
</head>
<body>
<p>are these 2 numbers equal? type yes or no in the box</p>
<p2>one</p2> and eight
<form action="welcome_get.php" method="get">
Answer: <input type="text" name="name"><br>
<input type="submit">
</form>
</body>
</html>
this is my new php page containing a previous answer and im having new errors
<html>
<body>
<?php
var_dump($_GET['name']);
$answer = $_GET['name'];
$saying = "congratulations";
if ($answer == "yes"){
echo $saying;
}
?>
</body>
</html>
the new errors are referring to my php page which is welcome_get.php
var_dump($_GET['name']);
Additionally, to see all available GET params:
var_dump($_GET)
To assign to a new variable:
if(!empty($_GET['name'])){
$answer = $_GET['name'];
if($answer == 'something'){
// do something
}
}
You can use isset function to check if $_GET['var'] is set
if(isset($_GET['name'])){
$answer = $_GET['name'];
$saying = "congratulations";
if ($answer == "yes"){
echo $saying;
}
}
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 am trying to enter a PHP 2D array variable as the value-attribute of a text input form in HTML.
Here is what I'm trying to do:
<?php
$test = array(array());
$test[0][0] = 123;
echo <<<_END
<html>
<head>
<body>
<form action = "test.php">
<input type = "text" value = "$test[0][0]">
</form>
<body>
<head>
<html>
_END;
?>
I get a warning saying: Notice: Array to string conversion in "path" on line 12,
and the form just displays Array[0].
What is happening here? How should this be done to get correct results?
Surround your variable with curly braces. So:
echo <<<_END
<html>
<head>
<body>
<form action = "test.php">
<input type = "text" value = "{$test[0][0]}">
</form>
<body>
<head>
<html>
_END;
It is best practice to surround your variables used inside of a string with curly braces. Especially array variables. This tells php that the variable name is everything between the curly braces.