How to get API Curl authorisation to work via PHP - php

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.

Related

Php using Curl to POST to a custom API

I'm working on a project and am attempting to link a Java application to my website/database via an API.
I'm running on an aws ec2 instance. The problem I am encountering is using Curl to POST data to another webpage. (The end-product will require proxies, and I've decided to use curl to manage this.)
The PHP code that makes the request:
<?php
function get_url($url)
{
$ch = curl_init();
if($ch === false)
{
die('Failed to create curl object');
}
// Temporary, just trying to get POST value to work.
$data = "password=temp";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data2 = curl_exec($ch);
curl_close($ch);
return $data2;
}
echo get_url("www.mywebsite.org/API/myscript.php");
?>
The server-side code that handles the request:
<?php
echo "Password: " . $_POST["password"];
?>
The issue I am having is with passing the post variables through curl and retrieving them.
The output on the page is:
Password:
and the POST variables are not being set. I'm thinking I am missing a CURLOPT somewhere but can't figure out which one!

curl how to format a curl request with an api key in php

hi am new to curl but need it for a particular project could you help me format this code to work i would like to get the results and print out the raw JSON on the page here is the code i am using
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "curl -u api key: https://api.companieshouse.gov.uk/search/companies");
$x = curl_exec($curl);
curl_close($curl);
print($x);
this is a link to the api page i am trying to use
https://developer.companieshouse.gov.uk/api/docs/search/companies/companysearch.html
this is the example they give on the page
curl -uYOUR_APIKEY_FOLLOWED_BY_A_COLON:
https://api.companieshouse.gov.uk/search/companies
these are the parameters for the call if possible i would like to set them as well
q (required)
items_per_page (optional)
start_index (optional)
I simplified your request.you can try this by using below code.
$params = array("q"=>"<Some Value>","items_per_page"=>"<Some Value>","start_index"=>"<Some Value>");
$curl = curl_init();
curl_setopt($curl, CURLOPT_USERPWD, "api key:");
curl_setopt($curl, CURLOPT_URL, "https://api.companieshouse.gov.uk/search/companies?".http_build_query($params));
$x = curl_exec($curl);
curl_close($curl);
print($x);
Thanks for this opportunity to learn more of curl :-)
I tested the code below, it works. Of course you must be registered on the site and you must have got your API key. You will then provide it as username/password, but without username (this is written here on the site).
$searchvalue= 'Test';
$curl = curl_init("https://api.companieshouse.gov.uk/search/companies?q=$searchvalue");
curl_setopt($curl, CURLOPT_USERPWD, "your_api_key_provided_by_the_site");
// curl_setopt($curl, CURLOPT_HEADER, true); // when debugging : to show returning header
$rest = #curl_exec($curl);
print_r($rest);
if(curl_errno($curl))
{
echo 'Curl error : ' . curl_error($curl);
}
#curl_close($curl);

I can't add an image Web Service Prestashop

When I try to add an image to a product I don't get any errors, but the image doesn't get added.
This is my code:
function addImage($idProduct)
{
$key = 'XXXXXXXXXXXXXXXXXXX';
$url = "http://192.168.1.81/api/images/products/".$idProduct;
$image_path = 'image2.jpg';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERPWD, $key);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => '#'.$image_path.';type=image/jpg'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
echo '<h2>Image Added</h2>';
}
I also made a change on PSWebServiceLibrary.php, because Prestashop Web Service API keeps asking for authentication. This is the link where I got the code Prestashop Web Service API keeps asking for authentication .
This is the code I added:
$url .= '&ws_key=' . $this->key;
The problem is that the code to add an image was working before I made that change on PSWebServiceLibrary.php, and I don't know how to solve it.
I am using prestashop 1.6.1.5
Any help will be appreciated.
Greetings!
I think it's only a missing of "?" in your url.
If I follow your $url logic the result will be for product of ID 1234567 by example :
http://192.168.1.81/api/images/products/1234567&ws_key=ZOEJFD3429JD209AZJX0DJF20
So your server waits this url to hanlde ws_key as GET parameter :
http://192.168.1.81/api/images/products/1234567?ws_key=ZOEJFD3429JD209AZJX0DJF20
You need to add this "?" at the end of you URL like this :
$url = "http://192.168.1.81/api/images/products/".$idProduct."?";
Best Regards,
TGA
For me works like this:
curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => curl_file_create($image_path)));

Getting images from Instagram, always null

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.

Why my Google endpoint is always the same?

always: https://www.google.com/accounts/o8/ud
i got wordpress openid ok. so i think is is just discovery phase got some probelms..
<?php $ch = curl_init();
$url = 'https://www.google.com/accounts/o8/id';
$url = $url.'?';
$url = $url.'openid.mode=checkid_setup';
$url = $url.'&openid.ns=http://specs.openid.net/auth/2.0';
$url = $url.'&openid.claimed_id=http://specs.openid.net/auth/2.0/identifier_select';
$url = $url.'&openid.identity=http://specs.openid.net/auth/2.0/identifier_select';
$url = $url.'&openid.return_to='.site_url().'/user/openid/login_callback';
$url = $url.'&openid.realm=http://www.example.com/';
// set url
curl_setopt($ch, CURLOPT_URL, $url);
//return the transfer as a string
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,array("Accept: */*"));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
// $output contains the output string
$xdr = curl_exec($ch);
if (!$xdr) {
die(curl_error($ch));
}
// close curl resource to free up system resources
curl_close($ch);
$xml = new SimpleXMLElement($xdr);
$url = $xml->XRD->Service->URI;
$request = $connection->begin($url);
$request always null...
Take a look at https://blog.stackoverflow.com/2009/11/google-offers-named-openids/ where Jeff explains this behavior and what the user can do about it:
Well, the good news is, now you can! Google just gave us a fantastic Thanksgiving Day present in the form of Google Profiles supporting OpenID. And with a Google Profile, you get to pick a named URL of your choice!
Your question has the right endpoint URL (the one ending in /ud), but your example code is sending the request to the identifier URL (/id), not the endpoint URL.
My above code do return https://www.google.com/accounts/o8/ud in $url, which is correct actually
the problem is, you do not need to use openid php lib, just redirect the user to https://www.google.com/accounts/o8/ud with query string like:
https://www.google.com/accounts/o8/ud?openid.mode=checkid_setup&......

Categories