I am using Laravel / PHP on the back end to communicate with the Forge system. To establish an initial token, I am using the guzzle tool kit. This works fine.
// Get environment variables
$FusionID = getenv('THISID');
$FusionSecret = getenv('THISSECRET');
// Make call to get token with authorization code, client id, and secret
$client = new Client(); //GuzzleHttp\Client
$response = $client->request('POST', 'https://developer.api.autodesk.com/authentication/v1/gettoken', [
'form_params' => [
'grant_type' => 'authorization_code',
'code' => $authCode,
'client_id' => $FusionID,
'client_secret' => $FusionSecret,
'redirect_uri' => 'https://www.example.com/redirect',
'scope' => array('data'=>'create', 'data'=>'read')
]
]);
$body = $response->getBody();
$obj = json_decode($body);
For the numerous other commands, the guzzle protocol seems to have issues so I have been using the straight curl commands. However, I can't seem to get either to work for the refresh token.
I have verified that the variables below have the proper data, but I get the error that
"developerMessage":"The required parameter(s) client_id,client_secret,grant_type not present in the request","userMessage":"","errorCode":"AUTH-008",
I am not sure what to do. Both the guzzle method and the curl method do not seem to be working for me.
$thumbData = '{"client_id":"'.$FusionID.'",
"client_secret":"'.$FusionSecret.'",
"grant_type":"refresh_token",
"refresh_token":"'.$userInfo->refresh_token.'"}';
$url = 'https://developer.api.autodesk.com/authentication/v1/refreshtoken';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $thumbData );
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded'
));
$response = curl_exec ($ch);
$err = curl_error($ch);
curl_close ($ch);
The way you post x-www-form-urlencoded data with Guzzle looks fine so that leaves the question to be whether your Guzzle version is 6+ otherwise use body instead of form_params as your payload option.
With cURL you request body should be delimited by & in x-www-form-urlencoded format like below instead of `json:
curl_setopt($ch, CURLOPT_POSTFIELDS,
"client_id=sb233&client_secret=2333&...");
Related
This's API request.
curl --location --request POST 'http://1455.api.123/xx' \
--header 'ApiPass: *******\
--header 'Content-Type: application/json' \
--data-raw '{
"jsonrpc": "2.0",
"id": "null",
"method": "person",
"params": {
"id": 15 // user ID in DB
}
}'
I'm use this code for call request.
public function call_data($id){
$apiKey = '***********';
$apiUrl = 'http://1455.api.123/xx';
$message = json_encode(
array('jsonrpc' => '2.0', 'id' => 'null', 'method' => 'person', 'params' => array('id'=>$id));
);
//$sign = hash_hmac('sha512', $message, $apiSecret);
$requestHeaders = [
'ApiPass:' . $apiKey,
'Content-type: application/json'
];
$ch = curl_init($apiUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $message);
curl_setopt($ch, CURLOPT_HTTPHEADER, $requestHeaders);
$response = curl_exec($ch);
curl_close($ch);
$arrayData = json_decode($response,true);
$arrayDataUsers = $arrayData;
return $arrayDataUsers;
}
How can I retrieve multiple id from api in once request where i don't want to use loop call_data()?
Note. One request for this code has load time about 0.5sec.
It depends on what capabilities the API provides you. If there is any API that allows you to access multiple IDs at once then you can use it, otherwise unfortunately you'll have to resort to loop only.
If you want to increase the speed of retrieving the results, you might try looking into parallel retrieval of the data but it totally depends on your use case on how you are using the user data ( or if the API limits your parallel requests or not. )
I have this Url : https://ipfind.co/?ip=188.225.179.138&auth=dde7cf52-2294-47e3-adb2-1e559099527e
when I request it from browser it gives me this response:
{"ip_address":"188.225.179.138","country":"Palestinian Territory, Occupied","country_code":"PS","continent":"Asia","continent_code":"AS","city":"Ramallah","county":null,"region":null,"region_code":null,"timezone":"GMT+2","owner":"COOLNET NEW COMMUNICATION PROVIDER","longitude":35.2,"latitude":31.9}
how can I invoke this url from laravel?
note that I tried this code, but it didn't work.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://ipfind.co/?ip=188.225.179.138&auth=dde7cf52-2294-47e3-adb2-1e559099527e');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec();
curl_close($ch);
dd($data);
Thanks in advance.
In the Curl you shall do this Easy 5 steps
Step 1
Initiate the Curl $cSession = curl_init();
Step 2
Define the Curl Url
curl_setopt($cSession,CURLOPT_URL,"https://ipfind.co/?ip=188.225.179.138&auth=dde7cf52-2294-47e3-adb2-1e559099527e");
curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
curl_setopt($cSession,CURLOPT_HEADER, false);
Step 3
Execute the Curl
$result=curl_exec($cSession);
Step 4
Close the Curl Connection
curl_close($cSession);
Step 5
Print the Result
echo $result;
Putting all together
<?php
$cSession = curl_init();
curl_setopt($cSession,CURLOPT_URL,"https://ipfind.co/?ip=188.225.179.138&auth=dde7cf52-2294-47e3-adb2-1e559099527e");
curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
curl_setopt($cSession,CURLOPT_HEADER, false);
$result=curl_exec($cSession);
curl_close($cSession);
echo $result;
?>
There is a PHP package for getting an IP address for IP Find that you can install with composer here that works great with Laravel.
https://github.com/tenfef/ipfind-php/
I've tried using the Javascript version of google places API, but now I'd like to move the code to server side with an API key. I've tried using all possible permutations and combinations of the curl request, but couldn't find a solution to it.
$url = urlencode('https://maps.googleapis.com/maps/api/place/autocomplete/json?input=' . $input . '&types=establishment&location=13.052415, 80.250824&radius=500&sensor=true&key=KEY');
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);
NOTE: I've used my app's KEY in the "key" params.
Response I get => bool(false)
Any suggestions?
If I follow the box api 2.0 curl example to get the metadata of a file, I get the information. When I append /data to the url to download the file, curl says:
curl: (52) Empty reply from server
I did this with two files, both text files, same result.
The system here is OpenBSD 5.0.
In python 3, the same request raises an exception that says that the reply from the server contained a status line that is empty.
I had the same issue. The problem is that you need the CURLOPT_FOLLOWLOCATION option set to true. Box will execute a redirection from the API call to the actual file. Without it, it will return an empty response.
An empty response should be returned in the event that the file itself is empty. If, for instance, the two text files don't have any text in them, no text will be returned i.e. this appears to be expected behavior.
I'm getting the same issue. I have uploaded a file successfully, retrieved the file ID, then when I try to download the file, nothing happens. Like the original poster mentioned, if I take off the "/data" portion of the URL, then I can get all of the info back about the file successfully.
Here is my "download code" that gets called from a form with a simple "Download File" button for that particular file ID.
$boxkey = "ThisIsMyAPIKey";
$auth_token = $_POST['auth_token'];
$url = "https://www.box.com/api/2.0/files/".$_POST['file_id']."/data";
$header = array("Authorization: BoxAuth api_key=".$boxkey."&auth_token=".$auth_token);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_exec($ch);
curl_close($ch);
$url = "https://api.box.com/2.0/files/$fileId/content?access_token=$accessToken";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($ch);
curl_close($ch);
I'm trying to retrieve a view with the (Node)Date Created argument. However I don't seem to be able to get it to work right, when I send it though cURL. Here is my code:
$method = 'views.get';
$hash = hash_hmac('sha256', $timestamp .';'.$domain .';'. $nonce .';'. $method, $api_key);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, 'http://site.com/services/json');
$date = $_GET['date'];
$data = array(
'method' => '"'. $method .'"',
'hash' => '"'. $hash .'"',
'domain_name' => '"'. $domain .'"',
'domain_time_stamp' => '"'. $timestamp .'"',
'nonce' => '"'. $nonce .'"',
'sessid' => '"'. $sessid .'"',
'view_name' => '"frontpage"',
'args' => '"'. $date .'"'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$json_result = curl_exec($ch);
I'm guessing its processing args somehow, as it returns [] with that, and when I remove 'args' =>.... it runs with the wildcard setting.
Have you tried using Views datasource? It prints out JSON, and you can still pass an argument to your view.
We have no way of knowing how you configured the date argument in your View, but it is certain that Views lets you configure a date argument:
Here's an example date range in Views.
And you can continue to use CURL to fetch the JSON from your View while using Views datasource as well. Then you could pass the date into the the request as such:
curl_setopt($ch, CURLOPT_URL, 'http://site.com/myjsonview/2011-01-11--2011-03-11');