i am writing new php post api how to get output.
URL : http://localhost/webservices/test.php
currently i am using postman for development purpose.when i get request as json i am able to handle.
JSON Example Request :
{"firstName":"Ram","lastName" :"R"}
$request = file_get_contents('php://input');
$parameters = json_decode($request,true);
$firstName = $parameters['firstName'];
echo $firstName ; // output will be Ram
Now My Question is how can i get reponse from Request Like This
firstName=Ram&lastName=R
Now how can Get First Name ?
Thanks in advance.
You can Use following Method to get Values from Header.
$fname = $_GET['firstName'];
echo $fname;
your Url will be like:
http://localhost/webservices/test.php?firstName=Ram&lastName=R
I'm working in to get the latitude and longitude from google maps through the geolocation API. I have tried many times but always is showing this error:
Trying to get property of non-object in C:\wamp64\www\index3.php on line 16
The issue is that I'm not able to get the info from the $url so then, cannot get the latitude and longitude.
I changed my code and now it is, but still with no working.
<php
$address = urlencode('BTM 2nd Stage,Bengaluru,Karnataka,560076');
$url = "https://maps.googleapis.com/maps/api/geocode/json?address=$address&key=*************************************";
$json = file_get_contents($url); // I tried with the # symbol and without it
$data=json_decode($json);
echo ($data->results[0]->geometry->location->lat);
?>
And also I used this one:
<php
$address = 'BTM 2nd Stage,Bengaluru,Karnataka,560076';
$encode = urlencode($address);
$key = "insert key here";
$url = "https://maps.googleapis.com/maps/api/geocode/json?address={$encode}&key={$key}";
$json = file_get_contents($url); // I tried with the # symbol and without it
$data = json_decode($json);
echo "Latitud".($data->results[0]->geometry->location->lat);
?>
Without the # symbol I have this error line
Warning: file_get_contents() has been disabled for security reasons in [...][...] on line 8
change http
to https in your request url.
As Abrucius, you msut use HTTPS or google will deny the request. You must also encode the address or else you will get a "Bad request" error.
<?php
$address = 'BTM 2nd Stage,Bengaluru,Karnataka,560076';
$encode = urlencode($address);
$key = "insert key here";
$url = "https://maps.googleapis.com/maps/api/geocode/json?address={$encode}&key={$key}";
$json = file_get_contents($url);
$data = json_decode($json);
echo ($data->results[0]->geometry->location->lat);
Outputs: 12.9082396
replace following code
$address = 'BTM 2nd Stage,Bengaluru,Karnataka,560076';
$url = "http://maps.googleapis.com/maps/api/geocode/json?address=$address&key=AIzaSyDjWI3J9CNlANoB5PXNKq3nz1ZbYeOrNGE";
to
$address = urlencode('BTM 2nd Stage,Bengaluru,Karnataka,560076');
$url = "https://maps.googleapis.com/maps/api/geocode/json?address=$address&key=AIzaSyDjWI3J9CNlANoB5PXNKq3nz1ZbYeOrNGE";
You have provided http in the url, it should be https. Try below code that will help you. You have to use urlencode() because api will not allow space in the address, so you have to encode.
$address = 'BTM 2nd Stage,Bengaluru,Karnataka,560076';
$address = urlencode($address);
$url = "https://maps.google.com/maps/api/geocode/json?address={$address}&key=***************************";
$resp_json = file_get_contents($url);
$resp = json_decode($resp_json, true);
echo "<pre>";print_r($resp["results"][0]['geometry']['location']['lat']);
Try this code will work for sure
WillParky93's answer seems to work fine for me. Maybe try following the next link to allow https wrappers:
How to get file_get_contents() to work with HTTPS?
...or post the error you're getting so we can help you out
I just solved this issue, I was getting this error because of a proxy configuration. I was testing into a network with a proxy configuration. So, the proxy is not allowing me to connect with the Google database. Then I tried to connect with a different network and it worked perfectly. Thank you guys for your aid!
Im new to php and tried to get a json object from the twitch API to retrieve one of its values and output it. i.e
i need to get the information from this link: https://api.twitch.tv/kraken/users/USERNAME/follows/channels/CHANNELSNAME
plus i need to to something so i can modify the urls USERNAME and CHANNELSUSERNAME. I want it to be a api to call for howlong user XY is following channelXY and this will be called using nightbots $customapi function.
the date i need from the json is "created_at"
Since we were able to clear out the errorsheres the final PHP file that works if anyone encounters similiar errors:
<?php
$url = "https://api.twitch.tv/kraken/users/" . $_GET['username'] . "/follows/channels/" . $_GET['channel'];
$result = file_get_contents($url);
$result = json_decode($result, true);
echo $result["created_at"];
?>
You have a typo in your code on the first line and you're not storing the result of your json_decode anywhere.
<?php
$url = "https://api.twitch.tv/kraken/users/" . $_GET['username'] . "/follows/channels/" . $_GET['channel'];
$result = file_get_contents($url);
$result = json_decode($result, true);
echo $result["created_at"];
You have to call the page this way page.php?username=yeroise&channel=ceratia in order to output the created_at value for this user and this channel.
In your code you're using 2 different ways to get the content of the page and you only need one (either file_get_contents or using CURL), I chose file_get_contents here as the other method adds complexity for no reason in this case.
I want to get request in a website like that
$id = "something";
$response = http_get("http://example.com?id=$id, array("timeout"=>1), $out);
I don't find how exactly I can use http_get
I've tried with file_get_contents, it is working like
$out = file_get_contents("http://www.example.com?id=something");
but I want it like that
$out = file_get_contents("http://www.example.com?id=$id");
this one is not working
The way I got it to work was using the code below.
$out = file_get_contents("http://www.example.com?id=".$id);
This will concatenate the $id variable contents onto the end of the url to request.
Could anyone help me to create search for movies using PHP when connecting to the Rotten Tomatoes API?
On the Rotten Tomatoes site they give you example code how to get content for specific movie like so:
<?php
$apikey = 'insert_your_api_key_here';
$q = urlencode('Toy Story'); // make sure to url encode an query parameters
// construct the query with our apikey and the query we want to make
$endpoint = 'http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=' . $apikey . '&q=' . $q;
// setup curl to make a call to the endpoint
$session = curl_init($endpoint);
// indicates that we want the response back
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// exec curl and get the data back
$data = curl_exec($session);
// remember to close the curl session once we are finished retrieveing the data
curl_close($session);
// decode the json data to make it easier to parse the php
$search_results = json_decode($data);
if ($search_results === NULL) die('Error parsing json');
// play with the data!
$movies = $search_results->movies;
echo '<ul>';
foreach ($movies as $movie) {
echo '<li>' . $movie->title . " (" . $movie->year . ")</li>";
}
echo '</ul>';
?>
I'm an beginner with PHP so an example would be great. I have manage to solve this with JavaScript but the server I have to host the page won't display it because of updates. So now I will have to turn to PHP because it displays the code from above too.
You need to create a form with a text field. Name the field q so it will work with your sample code.
Have the form post to the url of your sample php script.
Remove this line:
$q = urlencode('Toy Story');
Replace it with this:
$q = urlencode($_POST['q']);
That should get you started.
Note that no effort is made to sanitize the $_GET. I am just providing the basics you need to have a search form running based on the example code you posted.
If you do not know how to create a form, here is a link to a tutorial:
http://www.tizag.com/phpT/forms.php