How to cURL to PHP Curl PayQuicker - php

How can I transfer this cURL into php? It's not returning anything
Trying all the ways and not working. Do you know a library for codeigniter that not requires composer?
curl --location --request POST 'https://identity.mypayquicker.build/core/connect/token' \\\
--header 'Authorization: Basic {token}'
--data-urlencode 'grant_type=client_credentials' \
--data-urlencode 'scope=modify'
--data-urlencode 'scope=readonly'",
"language": "curl",
"name": " "
Doing this way
$header = array();
$header[] = 'Content-length: 0';
$header[] = 'Content-type: application/json';
$header[] = 'Authorization: Basic {token}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://identity.mypayquicker.com/core/connect/token");
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, TRUE);
// This option is set to TRUE so that the response
// doesnot get printed and is stored directly in
// the variable
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'grant_type' => 'client_credentials'
));
$data = curl_exec($ch);
var_dump($data);
Not Returning Anything

Related

File upload CURL command to PHP Curl

I have CURL command I need to use him in php page how I can change it. I have no idea about it.
The command :
curl --request POST \
--url 'https://api.sirv.com/v2/files/upload?filename=%2Fpath%2Fto%2Fuploaded-image.jpg' \
--header 'authorization: Bearer BEARER_TOKEN_HERE' \
--header 'content-type: image/jpeg' \
--data "#/path/to/local-file.jpg"
For your curl command, Please try this PHP :
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.sirv.com/v2/files/upload?filename=%2Fpath%2Fto%2Fuploaded-image.jpg');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$post = array(
'file' => '#' .realpath('/path/to/local-file.jpg')
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$headers = array();
$headers[] = 'Authorization: Bearer BEARER_TOKEN_HERE';
$headers[] = 'Content-Type: image/jpeg';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
?>
Please also study the reference provided by Professor Abronsius if you have time. It's worth the effort.

How to use the access_token until its expiration and then get and use a new one with the refresh_token?

For getting connected to an API endpoint, it is necessary to include in the request a token, which should be received before.
Example:
curl -X GET \
https://api.mysite/v2/orders \
-H 'Authorization: Bearer <token>'
In PHP this will look like this:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.mysite/v2/orders');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$headers = array();
$headers[] = 'Authorization: Bearer <token>';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
$result = json_decode($response, true);
...
Authorization (OAuth2)
Fetch access token
curl -X POST \
https://api.mysite/v1/token \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'cache-control: no-cache' \
-d 'username=<YourUsername>&password=<YourPassword>&grant_type=password&client_id=token-mysite-api'
PHP version:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.mysite/v1/token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "username=<YourUsername>&password=<YourPassword>&grant_type=password&client_id=token-mysite-api");
$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
$headers[] = 'Cache-Control: no-cache';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
$result = json_decode($response, true);
$access_token = $result['access_token'];
$refresh_token = $result['refresh_token'];
The response looks like this:
{
"access_token": "<access tocken>",
"expires_in": 1800,
"refresh_expires_in": 7200,
"refresh_token": "<refresh tocken>",
"token_type": "bearer",
"not-before-policy": 0,
"session_state": "916a880a-9dae...",
"scope": "pumba-roles-app-portal partner email profile"
}
Once the access token is expired should be used the refresh token to get a new access token, without using users credentials again:
curl -X POST \
https://api.mysite/v1/token \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'cache-control: no-cache' \
-d 'refresh_token=<refresh_token>&grant_type=refresh_token&client_id=token-mysite-api'
PHP version:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.mysite/v1/token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "refresh_token=<refresh_token>&grant_type=refresh_token&client_id=token-mysite-api");
$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
$headers[] = 'Cache-Control: no-cache';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
$result = json_decode($response, true);
...
The response from refresh is the same as from getting token (see it above).
My question is:
How to do the verification and get the correct token? If the access_token is not expired > use it again. If it is expired > use the refresh_token for getting the new access_token and use it. Repeat this action for the next requests.
I spent much time trying different approaches but with each request, I get always new token.
Waiting for your help, please :).

How to send file for uploading using Php Curl Without Input type File Control?

I want to upload file from one server to another without using the file control and using Curl Request..
curl -X POST --header 'Content-Type: multipart/form-data' --header 'Accept: application/json' --header 'api-token: 6a53bcbe222c490196e4b9f87ba9148c' --header 'api-secret: 6a53bcbe222c490196e4b9f87ba9148c' -F "document[]=#C:/xampp/htdocs/project/image_name.ext" -F "document[]=#C:/xampp/htdocs/project/image_name.ext" -F "document[]=#C:/xampp/htdocs/project/image_name.ext" 'https://server_api_url.com
'
The above curl works fine...
But i dont know how to post the file in php ...
Below is sample code how i am sending post data using php curl
<?php
$doc_file_path_string = ' -F document[]=#image_name';
array_push($doc_data_array, $doc_file_path_string);
$url = 'https://server_url.com';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, implode(' ',$doc_data_array));
$headers = array();
$headers[] = "Content-Type: multipart/form-data";
$headers[] = "Accept: application/json";
$headers[] = "Api-Token: api_key";
$headers[] = "Api-Secret: api_key ";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
echo $result = curl_exec($ch);
?>
Here is how i did it
$url = <some url>;
$ch = curl_init();
$headers = ["Content-Type:multipart/form-data"];
// Define curl options
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
// Here we make base64 encode for file content and push it into curl
$base64 = base64_encode(file_get_contents(<path to file>));
curl_setopt($ch, CURLOPT_POSTFIELDS, ['file' => $base64]);
// Make request
$response = curl_exec($ch);
curl_close($ch);

Equivalent curl request in php

Im trying to get curl -X GET --header "Accept: application/json" --header "authorization: Bearer <API token>" "https://api.clashofclans.com/v1/clans?name=%23P8PQ8VL2"in php
so far i came up with this
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.clashofclans.com/v1/clans?name=%23P8PQ8VL2");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'authorization: Api key'
));
$result=curl_exec($ch);
curl_close($ch);
var_dump(json_decode($result, true));
this is the 1st time im using curl in php, thanks in advance :)
Hey thank you all i got it work. i had to use
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
Your are overwriting the header with the second set of the options. I would build up the header array like so:
$headers = array();
$headers[] = "Accept: application/json";
$headers[] = "authorization: Bearer " . $yourAPItoken;
$headers[] = 'Content-Type: application/x-www-form-urlencoded; charset=utf-8';
And send the array:
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

Create a cURL request using PATH API

Guys. I am new to CURL so i have no experience on implementing CURL request. In this case, i want to post some data using CURL. Here is the CURL :
curl -H 'Authorization: Bearer <ACCESS_TOKEN>' \
-H 'Content-Type: application/json' \
-d '{"source_url": "http://url/to/photo.jpg", "caption": "I like cheese!"}' \
https://partner.path.com/1/moment/photo
is there any of you guys know about implementing CURL request using the above data? Thank you very much.
finally, i found the answer. Here is how i create a request to the Path API
$url = 'https://partner.path.com/1/moment/photo';
$authorization = "Authorization: Bearer 8edf232243d58e4940d931490e882123432434f";
$headers = array($authorization,'Content-Type: application/json');
$json_data = '{"source_url": "http://url/to/photo.jpg", "caption": "I like cheese!"}';
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch,CURLOPT_POSTFIELDS, $json_data);
$result = curl_exec($ch);
curl_close($ch);
print_r( $result );

Categories