Rest POST API With file_get_contents Not Working IN PHP - php

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

Related

Httpful JSON inside an array

I've been trying for 3 days to get PHP to show the ID from this test JSON using PHP and httpful.
Anyone have any ideas as i've tried loads of different combinations and even tried created a handler to decode as an array... I just suck at PHP ?
// Make a request to the GitHub API with a custom
// header of "X-Trvial-Header: Just as a demo".
<?php
include('\httpful.phar');
$url = "https://jsonplaceholder.typicode.com/posts";
$response = Httpful\Request::get($url)
->expectsJson()
->send();
echo "{$response[0]['id']}";
?>
My output... still nothing
You are not properly indexing the return value.
The response is a mixed value. So you should do something like below to get what you are looking for.
<?php
include('\httpful.phar');
$url = "https://jsonplaceholder.typicode.com/posts";
$response = Httpful\Request::get($url)
->expectsJson()
->send();
echo $response->body[0]->id;
?>

Get a JSON from url with PHP

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.

Requesting User Info with Foursquare API

I am using the Foursquare API to request information for my users.
It says to make a request to get user info like this,
https://api.foursquare.com/v2/users/self?oauth_token=TOKENHERE
So I am doing it like this,
$fsUser = file_get_contents("https://api.foursquare.com/v2/users/self?oauth_token=".$access_token);
When I var_dump I get null from the value I am saving it in, Here is what my full code looks like,
<?php
$client_id = "iwdhwouchweohcuwoehcowehcu";
$secret = "ojdwojwjwrhvo";
$redirect = "http://www.example.com";
if($_GET['code']){
//We need to hit up the authkey URL and get the key in JSON format
$authkey = file_get_contents("https://foursquare.com/oauth2/access_token?client_id=".$client_id."&client_secret=".$secret."&grant_type=authorization_code&redirect_uri=".$redirect."&code=".$_GET['code']);
//We then need to decode it and store that key in a variable
$auth = json_decode($authkey,true);
$access_token = $auth['access_token'];
//we then look up whatever endpoint of the api we want
$fsUser = file_get_contents("https://api.foursquare.com/v2/users/self?oauth_token=".$access_token);
$user = json_decode($fsUser, true);
// $name = $decoded_userinfo['response']['user']['firstName'];
}
?>
When I var_dump $fsUser I get bool(false)
I have var_dump every variable with no issues till I get to $fsUser I can not get past this part...
You also need to use the version number (v) with your request.
Like this
https://api.foursquare.com/v2/venues/search?client_id=CLIENT_ID&v=20140806&m=foursquare
See documentation here.
https://developer.foursquare.com/overview/versioning

Can't get Last.FM API PHP Proxy to work

I'm trying to regularly retrieve JSON album data from the Last.FM API, and since they don't support JSONP I'm using a PHP Proxy to circumvent the cross-domain limitation. Here's my PHP proxy:
<?php
$artist = $_GET["artist"];
$album = $_GET["album"];
$content = file_get_contents('http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=APIKEY=' . $artist . '&album=' . $album . '&format=json');
echo $content;
?>
As you can see I'm using GET to specify the artist and album, so the URL would look like albumartproxy.php?artist=Jake%20Bugg&album=Jake%20Bugg
But for some reason I get the following error when I try to run the proxy:
Warning: file_get_contents(http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=037358e302c80571663e6a7a66b1dc05&artist=Jake Bugg&album=Jake Bugg&format=json) [function.file-get-contents]: failed to open stream: HTTP request failed! in /MYDOMAIN/albumartproxy.php on line 6
When I access the API URL directly it works? Can someone help? What am I doing wrong?
You'll need to urlencode() the GET parameters that you are passing along with the request.
You need urlencode() the GETs, like so:
<?php
$apikey = "273f3dc84c2d55501f3dc25a3d61eb39";
$artist = urlencode($_GET["artist"]);
$album = urlencode($_GET["album"]);
$autocorrect = 0;
$content = "http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=".$apikey."&artist=".$artist."&album=".$album."&autocorrect=".$autocorrect;
echo file_get_contents($content);
?>
Sample output: http://pastie.org/8114551
Hope this helps!

How to get a value of a second level JSON node using PHP from a Ajax request?

How can I get a value of a second level JSON node using PHP from a Ajax request?
If I have the next JSON data in client:
var Data = {idJS: "1", dataToSet: "example", another:{ field1: "example2"} };
When the Ajax request is done, in PHP, dataToSet is get as follows: $_POST['dataToSet'], in particular, I use Codeigniter, then I use $this->input->post('dataToSet'). But, How can I get another->field1?
[Solved]:
$postdata = $this->input->post();
$postdata['another']['field1'];
I don't know how you do it with codeigniter, but you have to decode the json. Afterwards you can acces the field. This might look something like this:
$postdata = json_decode($this->input->post);
$postdata['another']['field1];
This should do it:
$data = $this->input->post('another');
print_r($data->field1);
// or you can do it in one line:
$this->input->post('another')->field1;
Try this:
$ata = json_decode($this->input->post('dataToSet'), true);
$field1 = $data['another']['field1'];

Categories