How can I retrieve multiple id from api in one request? - json

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. )

Related

Autodesk Forge token reauthorization command doesn't recognize parameters

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&...");

json_decode doesn't work with string

With this code :
$url = 'https://www.xxxxxxx.com/api/v1/phone/?apikey=xxxxxxxxxx&id='.$id;
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_URL, $url);
curl_setopt($cURL, CURLOPT_HTTPGET, true);
curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Accept: application/json'
));
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($cURL);
curl_close($cURL);
echo $result;
var_dump ($result);
var_dump(json_decode($result, true));
I get that :
{"telephone":"0 811 228 338"}
string(32) "{"telephone":"X XXX XXX XXX"}"
NULL
I don't understand why json_decode doesn't work here?
Thanks for helping me
with the little info i got i believe its a json formating problem.
<?php
$json = '{"foo-bar": 12345}';
$obj = json_decode($json);
print $obj->{'foo-bar'}; // 12345
?>
the json quotes are not valid
http://php.net/manual/en/function.json-decode.php
try it in some validator as this one http://jsonlint.com/
The solution was that the response is in UTF-8 but not UTF-8 (SANS BOM).
It means that there is a invisible caractere on the beginning of the string (U+FEFF).
The solution : $result = substr($result, 3);
Thanks for your help

request a web service from laravel that returns a json object

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/

Sending files in curl request with header application-json

I am trying to send data in json format in curl request. so my body is like this
$body = json_encode($array);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($body),
'Content-Md5: ' . base64_encode(md5($body, true))
));
I can set content header this way --header "Content-Type: application/json" But when my content body will have files to be sent, will content type header application/json work?
Do i need to change the content-type header if my body has file in it?
No and yes, if you are talking about simulating a browser to "select" files and "upload" them to the server then the header will change to
Content-Type: multipart/form-data;
if on the other hand you are talking about using text files in place of long strings on the command line then no.
Read the notes in the answers at send/post xml file using curl command line

Sending Arguments to Drupal Views (JSON, cURL)

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');