File upload CURL command to PHP Curl - php

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.

Related

How to cURL to PHP Curl PayQuicker

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

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

Convert command cURL to PHP cURL

I've never done any curl before so am in need of some help. I've tried to work this out from examples but cannot get my head around it!
How can I translate this curl command so that it works in a PHP script?
curl -v -H "Content-Type: text/xml" --cert WST279925._.1.pem --key WST279925._.1.key --pass fryeBu23^< -u WST279925._.1:KaWwNI31=$ --url https://test.ipg-online.com/ipgapi/services -d #request.xml --trace-ascii "trace.log" -o "response.xml" --insecure
My code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://test.ipg-online.com/ipgapi/services');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$post = array(
'file' => '#' .realpath('request.xml')
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_USERPWD, 'WST279925._.1' . ':' . $_ENV["KaWwNI31="]);
$headers = array();
$headers[] = 'Content-Type: text/xml';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
how do i add digital certificate and key?
Thanks for any help.

CURL works with Terminal but showing an error within PHP code?

When I use the terminal for posting an image using CURL it works fine but when I transform it within PHP code it throws an error 400 Bad Request.
CURL code using Terminal
curl -H "Authorization: Bearer ${access_token}" \
-H "Content-Type: image/jpeg" \
-i \
-X POST \
--data-binary #my_image.jpg \
https://example.com/objects/v1/files
CURL code within PHP file
$headers = array(
'Content-Type: image/jpeg',
'Authorization: Bearer ' . $accessToken,
);
$binary = 'my_image.jpg';
$ch = curl_init('https://example.com/objects/v1/files');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, '#'.$binary);

Trouble converting POST curl from command line to php

I am having trouble converting my curl command into php.
This part works great.
CURL command that adds an entry into my Parse.com database:
curl -X POST \
-H "X-Parse-Application-Id: my_id" \
-H "X-Parse-REST-API-Key: api_id" \
-H "Content-Type: application/json" \
-d "{\"SiteID\":\"foundID\",\"dataUsedString\":\"foundUsage\",\"usageDate\":\"foundDate\", \"monthString\":\"foundMonth\", \"dayString\":\"foundDay\",\"yearString\":\"foundYear\"}" \
https://api.parse.com/1/classes/MyClass
SOLVED ANSWER:
I have created this php script to replicate the command:
<?php
$ch = curl_init('https://api.parse.com/1/classes/MyClass');
curl_setopt($ch,CURLOPT_HTTPHEADER,
array('X-Parse-Application-Id:my_id',
'X-Parse-REST-API-Key:api_id',
'Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"SiteID\":\"foundID\",\"dataUsedString\":\"foundUsage\",\"usageDate\":\"foundDate\", \"monthString\":\"foundMonth\", \"dayString\":\"foundDay\",\"yearString\":\"foundYear\"}");
curl_exec($ch);
curl_close($ch);
?>
You've missed some crucial configurations.
These are the set the CURL to send request using POST, and the second is data to send. (RAW DATA is being sent as string into POSTFIELDS, those if you send array - it will automatically append header "multipart/form-data"
$ch = curl_init('https://api.parse.com/1/classes/MyClass');
curl_setopt($ch,CURLOPT_HTTPHEADER,
array(
'X-Parse-Application-Id:my_id',
'X-Parse-REST-API-Key:api_id',
'Content-Type: application/json'
)
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"SiteID\":\"foundID\",\"dataUsedString\":\"foundUsage\",\"usageDate\":\"foundDate\", \"monthString\":\"foundMonth\", \"dayString\":\"foundDay\",\"yearString\":\"foundYear\"}");
curl_exec($ch);
curl_close($ch);
HTH:)
Since you are doing a POST request you need to tell Curl to do that as well:
$postData = '{"SiteID":"foundID","dataUsedString":"foundUsage","usageDate":"foundDate", "monthString":"foundMonth", "dayString":"foundDay","yearString":"foundYear"}';
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
You may also need to supply a Content-Length header:
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-Parse-Application-Id: my_id',
'X-Parse-REST-API-Key: api_id',
'Content-Type: application/json',
'Content-Length: '.strlen($postData))
);

Categories