I develop a small script on localhost (WAMP server, CURL enabled) to retrieve images from Instagram. Based on this script: see tutorial I have also set up the variables on Instagram. I'm not sure what is wrong more specifically or how to solve it (probably with authentication).
USER ID
CLIENT ID
CLIENT SECRET
WEBSITE URL
REDIRECT URI
This is my user
http://instagram.com/krondorl
function fetchData($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$result = fetchData("https://api.instagram.com/v1/users/280766222/media/recent/?access_token=df04c6989eaf4490bdac1f82554182bb");
$result = json_decode($result);
var_dump($result);
Try the code below which works for me. Make sure your URL is correct.
$json_url ='https://api.instagram.com/v1/users/280766222/media/recent/?access_token=df04c6989eaf4490bdac1f82554182bb';
// Initializing curl
$ch = curl_init( $json_url );
// Configuring curl options
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-type: application/json')
);
// Setting curl options
curl_setopt_array( $ch, $options );
// Getting results
$results = curl_exec($ch); // Getting jSON result string
$json_decoded = json_decode($results);
By the way I check your URL which happens to be incorrect and give me the below error.
{"meta":{"error_type":"OAuthAccessTokenException","code":400,"error_message":"The \"access_token\" provided is invalid."}}
Before you trying the code, try the URL on the browser and see whether the URL is correct.
Related
Here are the codes:
<?php
class General{
#to verify user password with api
public static function verifyUser($user,$pswd){
#url
$url = 'https://api.hp.upm.edu.my/cron/request/verify.php';
# initiate cur
$ch = curl_init($url);
# create json data
$jsonData = array(
'jtoken' => "test123",
'id' => "$user",
'pass'=> "$pswd"
);
#Encode the array into JSON.
$jsonDataEncoded = json_encode($jsonData);
#Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);
#Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
#Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
#Execute the request
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}
?>
The passed data are username and password, identified by $user and $pswd respectively
The problem is, the end result should return something.
But I found it empty, after checking.
So, what I learned so far are:
I checked the user and password, they are all intact
when I print $ch during "initiate cur", it prints something
when I printed $ch
However, when I printed $result after curl_exec, it is empty.
What do I do? Where is my mistake? Is it the problem of the api url or soemthing else? Please, I need your help
Edit:
Someone suggested that using json_decode will help with this. But, I have already implemented json_decode, after $result is returned.
$result = General::verifyUser($_POST['user'], $_POST['pass']);
$obj = json_decode($result, true);
Above is the method call that passes the username and password to the "General" class.
I have printed curl_error for $ch and it says:
"Could not resolve host: api.hp.upm.edu.my"
I am working with dr chrono API and am trying to initialized the First step which is authorisation
The API documentation is here.
Here is an Authorisation sample:
https://drchrono.com/o/authorize/?redirect_uri=REDIRECT_URI_ENCODED&response_type=code&client_id=CLIENT_ID_ENCODED&scope=SCOPES_ENCODED
Here is what I have tried:
1.) I have tried the first code below
<?php
echo "<a href='https://drchrono.com/o/authorize/?redirect_uri=https://example_site.com/return_page.php&response_type=code&client_id=myclient-id-goeshere&scope=BASE_SCOPE:[read|write]'>Authorize</a>";
?>
but when the page redirects it displays error of Invalid_scope. Below is the error link returned.
https://example_site.com/return_page.php?error=invalid_scope
2.) Using Curl
$ci = 'my-client-id-goes-here';
$ci_encode = urlencode($ci);
$uri = 'https://example_site.com/return_page.php';
$uri_encode = $uri;
$url = "https://drchrono.com/o/authorize/";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
'response_type'=>'code',
'client_id'=>$ci_encode,
'redirect_uri'=>$uri_encode,
'scope'=>'BASE_SCOPE:[read|write]',
]));
$response = curl_exec($ch);
curl_close($ch);
print_r($response);
Using curl code above does even redirect me at all.
I think the major problem is the scope not properly set. How can I solve this issue?
Update
Code section for curl:
$ci = 'my-client-id';
$ci_encode = urlencode($ci);
$uri = 'https://example_site.com/return_page.php';
$uri_encode = $uri;
$url = "https://drchrono.com/o/authorize/?";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
'response_type'=>'code',
'client_id'=>$ci,
'redirect_uri'=>$uri,
'scope'=>'patients:summary:read patients:summary:write calendar:read calendar:write clinical:read clinical:write'
]));
$response = curl_exec($ch);
curl_close($ch);
print_r($response);
Instead of BASE_SCOPE:[read|write] I would use patients:summary:read patients:summary:write calendar:read calendar:write clinical:read clinical:write
The docs say that you can choose among user, calendar, patients, patients:summary, billing, clinical and labs and compose a scope out of these values.
I need get photos and descriptions from registered instagram.
As I understand from docs on github https://github.com/cosenary/Instagram-PHP-API
You need put auth link first ant then using auth token after click on it you are redirected to some callback url, where you can get media then. But can I somehow login automatically without clicking link??
I have some
$instagram = new Instagram(array(
'apiKey' => 'apiKey',
'apiSecret' => 'apiSecret',
'apiCallback' => 'apiCallback there'
));
with my data get after registration in https://www.instagram.com/developer/. How can I get what I want?
I guess try something like this:
$userid = "User-ID-Goes-Here";
$accessToken = "Token-Goes-Here";
// Gets our data
function fetchData($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
// Pulls and parses data.
$result = fetchData("https://api.instagram.com/v1/users/{$userid}/media/recent/?access_token={$accessToken}");
$result = json_decode($result);
//use data here
Source: http://blueprintinteractive.com/blog/how-instagram-api-fancybox-simplified
I have some short URLs. How can I get the original URLs from them?.
As the URL-shortener-services are mostly simple redirectors they use the location header to tell the browser where to go to.
You can use PHP's own get_headers() function to get the appropriate header:
$headers = get_headers('http://shorten.ed/fbsfS' , true);
echo $headers['Location'];
Try this
<?php
$url="http://goo.gl/fbsfS";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$a = curl_exec($ch); // $a will contain all headers
$url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); // This is what you need, it will return you the last effective URL
echo $url; // Redirected url
?>
You can use the curl functions for this:
// The short url to expand
$url = 'http://goo.gl/fbsfS';
// Prepare a request for the given URL
$curl = curl_init($url);
// Set the needed options:
curl_setopt_array($curl, array(
CURLOPT_NOBODY => TRUE, // Don't ask for a body, we only need the headers
CURLOPT_FOLLOWLOCATION => FALSE, // Don't follow the 'Location:' header, if any
));
// Send the request (you should check the returned value for errors)
curl_exec($curl);
// Get information about the 'Location:' header (if any)
$location = curl_getinfo($curl, CURLINFO_REDIRECT_URL);
// This should print:
// http://translate.google.com.ar/translate?hl=es&sl=en&u=http://goo.gl/lw9sU
echo($location);
For all services there is an API, which you can use.
I am using an API that returns JSON from a GET request
Eg.
https://api.domain.com/v1/Account/{auth_id}/Call/{call_uuid}
Returns
{
"call_duration": 4,
"total_amount": "0.00400"
}
How can I call this page from within a script and save call_duation and total_amount as separate variables?
Something like the following?:
$call_duration =
$_GET[https://api.domain.com/v1/Account/{auth_id}/Call/{call_uuid}, 'call_duration'];
$_GET[] contains the get parameters that are passed to your code - they don't generate a GET request.
You could use curl to make your request:
$ch = curl_init("https://api.domain.com/v1/Account/{auth_id}/Call/{call_uuid}");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$result = json_decode($output);
If PHP has allow_url_fopen enabled you can simply do
json_decode(file_get_contents('https://api.domain.com/v1/Account/{auth_id}/Call/{call_uuid}'))
Otherwise you'll have to resort to using something like Curl to get the request going. $_GET is a superglobal array which doesn't actually do anything. It only contains what the script was started with. It does not make any requests itself.
Use curl to get the JSON, then json_decode to decode it into PHP variables
$auth_id = 'your auth id here';
$call_uuid = 'your call_uuid here';
// initialise curl, set URL and options
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.domain.com/v1/Account/{$auth_id}/Call/{$call_uuid}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
// get the response and decode it
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response);
$call_duration = $response['call_duration'];
$total_amount = $response['total_amount'];