PHP get Response from a requested URL - php

I am implementing a OneClick Login function. From a existing function I am getting a URL, So to get the token I should send it, when I copy paste to the browser I am getting the token. I am using php so what is the way to get the token from PHP code.

You can use file_get_contents like this:
<?php
$response = file_get_contents("https://your-url.com/");

Related

How make redirection with web service?

I have a web-service which send JSON object as response to log my user.
But if I take the URL of the service and I put it on my navigator I see the json object, how I can send the json througth a redirection ?
Example :
Facebook login -> if we take the action in the form that call facebook/login?...
If facebook use JSON to send a response of the log i must see the JSON object if i call facebook/login?... but I was redirected to the main page, how that works ?
Thank for your reply.
The answer is to check the $_SERVER['HTTP_X_REQUESTED_WITH'] after sending an specific header in the AJAX request.
I have a last question in like :
Can i use a same like this the volley library, to allow the access to a php script only for an android app ?
You can see an example on header in this link :
https://gist.github.com/FabrizioCaldarelli/ad1118e5d8ab0661ba36

Parsing JSON in PHP is not giving response

So I am trying to get some information from a JSON file. But for some reason there is no actual response with the information that I need.
This is the code im running.
$json_object = file_get_contents('http://steamcommunity.com/profiles/'.$steamID.'/inventory/json/730/2');
$json_decoded = json_decode($json_object);
echo $json_decoded->success;
I get the steamID from a cookie. But if you want to see how the JSON looks, then you can check out this link:
http://steamcommunity.com/profiles/76561198031313244/inventory/json/730/2
The problem is that the URL you are checking is redirected to another URL, file_get_contents() doesn't support following redirects AFAIK, so you should better use cURL
Check this answer https://stackoverflow.com/a/4324018/5658508
the problem is like the others tell you is in this link http://steamcommunity.com/profiles/76561198031313244/inventory/json/730/2
i just monitor the response, so when you visit the link you get a 302 Moved Temporarily http response to the new link which is http://steamcommunity.com/id/SANDISS/inventory/json/730/2
you can try to get contents using file_get_contents if you have the link not just a steam id, or use curl library

Sending a call to facebook graph api does not return anything when an error occurs

I am using graph api to get a list of albums of a user. Now in case the user signs out or has changes password or any error condition, I do not get any response whereas some error code should be returned. The graph API url when opened in the browser shows the error but on using file_get_contents() i get error 400. On using curl, I get an empty response. Is there any way that I can find out when an error occurs and the reason for it?
The graph API url when opened in the browser shows the error but on using file_get_contents() i get error 400.
Well, that is the HTTP status code that Facebook sends with Oauth errors – nothing wrong about that.
But file_get_contents by default does not show you the actual HTTP response body content in case of an HTTP status codes that signals an error …
… which is just another reason, why one should use the official PHP SDK, which handles all of that nicely and presents you with an exception that tells you exactly what is up.
So do it, please.
(Even with file_get_contents one can get the body content of the response, when setting a “stream context” with the according option first. But using the PHP SDK is way simpler, so again: Please do it.)
I used curl instead of file_get_contents and the issue got resolved.
file_get_contents() gave me error many times when I put https instead of http or If I forgot to put any of them.
Your URL from which you get the content must have "http" at the starting, like:
file_get_contents("http://www.mysite.com");
If this solves it?

How to GET remote JSON or XML API data from within PHP and assign a return object as PHP variable?

What I'm doing:
I'm writing a custom program in PHP which pulls data via API from an online LMS service. Right now, I'm trying to implement the available single-sign-on functionality.
This part of the program needs to execute a GET request to the API when a button is clicked (via js or php POST or ?) and ultimately redirect the users browser to a URL which is supplied in the response from the API.
The API allows the choice of an XML or JSON response and I would prefer to use JSON but will make do with XML if needed.
From the API documentation on making requests:
All requests listed in this document should contain a content-type
(XML or JSON) in the request header and be prefixed with the following
base Uri: https://api.example.com/v1.svc
E.g. The Uri to GET a list of Users in XML format would be:
Content-Type: text/xml
GET https://api.example.com/v1.svc/users?apikey=MY-KEY&source=MY-APP
Below is what I'm trying to implement:
How to get the a user's LoginKey
Once you have the user id that you want to sign on you need to make a
GET request to /users/{user-id} which will return information about
the user. Included in this is a LoginKey which you can use to redirect
the user's browser to.
eg.
GET
https://api.example.com/v1.svc/users/USER-ID?apikey=YOUR_API_KEY&source=sampleapp
Response from API:
<User>
<Id>abc12345678</Id>
<UserName>rich_demo#example.com</UserName>
<FirstName>Rich</FirstName>
<LastName>Chetwynd</LastName>
.....
<LoginKey>https://demo.example.com/login.aspx?loginkey=xxxzzzyyy777222</LoginKey>
</User>
The <LoginKey> object data is the URL which I need to ultimately redirect the user's browser to.
I am new to working with APIs and have tried a ton of methods which I could not get to work before posting. If you know how to accomplish this I would be very grateful if you shared your knowledge.
Thanks.
From a HTML <form>, use a traditional post (not AJAX) to a PHP script that does this:
if(isset($_POST['userid']))
{
$userId = (int)$_POST['userid'];
$obj = simplexml_load_file('https://api.xxx.com/v1.svc/users/' . $userId . '?apikey=YOUR_API_KEY&source=sampleapp');
if($obj && isset($obj->LoginKey))
{
$loginKey = $obj->LoginKey;
header('Location: ' . $loginKey);
}
else
{
// failed to load the xml
}
}
If you want to do it with JSON you can use file_get_contents() to get the raw JSON from a URL, then use json_decode() to turn it into an object.
Also, if you want to do it via AJAX, you will have to echo the URL with PHP instead of trying to redirect, then have Javascript do the redirect with window.location.href = '...'

PHP HTTP request to get JSON response

I am a novice in PHP. I have a URL and I need generate a GET request to this URL and get a JSON response. How might I achieve this?
You can perform GET requests using the following. . . provided PHP is not in safe mode.
file_get_contents();
curl();
You can use http://php.net/curl library to send GET requests.

Categories