I Have the following command I can run in PHP and it works as expected:
$params = [
'file' => $xmlLocalPath,
'type' => 'mismo',
'file_type' => $xmlFile['file_type'],
'filename' => $xmlFile['file_name']
];
$cmd =
'curl -X POST ' . $this->outboundBaseURL . 'document \
-H "Accept: application/json" \
-H "Authorization: Bearer ' . $this->token . '" \
-H "Cache-Control: no-cache" \
-H "Content-Type: multipart/form-data" \
-F "file=#' . $params['file'] . ';type=' . $params['file_type'] . '" \
-F "type=' . $params['type'] . '" \
-F "filename=' . $params['filename'] . '"';
exec($cmd, $result);
I need to get this to work using PHP's curl library, but I can't get it to work quite right. I'm on PHP 5.6, and here's what I have right now:
$params = [
'file' => $xmlLocalPath,
'type' => 'mismo',
'file_type' => $xmlFile['file_type'],
'filename' => $xmlFile['file_name']
];
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $this->outboundBaseURL . 'document',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 60,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $params,
CURLOPT_HTTPHEADER => [
'Accept: application/json',
'Authorization: Bearer ' . $this->token,
'Cache-Control: no-cache',
'Content-Type: multipart/form-data'
]
]);
curl_setopt($ch, CURLINFO_HEADER_OUT, true); // enable tracking
$response = curl_exec($ch);
$err = curl_error($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
I know the issue has to be with POSTFIELDS, and the file in particular, but I'm not show how to give PHP CURL the parameters in a proper way such that the file and other parameters will send just as the do in the raw curl call. I understand that the "#" method is deprecated in PHP Curl and I've also tried using curl_file_create with the local file with no success
Try adding one more parameter:
CURLOPT_POST => 1
Related
I have collection on postman and it is working fine. But now, I am trying to convert this PHP cURL
curl --location --request POST 'https://test.url.com' \
--header 'Content-Type: application/x-www-form-urlencode' \
--header 'Authorization: Basic 1234567890' \
--data-urlencode 'abc_id=123456' \
--data-urlencode 'grant_type=client_credentials' \
--data-urlencode 'scope=req-auth/all'
Here is my code:
$headers = [
"Authorization: Basic 1234567890",
"Content-Type: application/x-www-form-urlencode"
];
$data = [
"abc_id" => '123456',
"grant_type" => 'client_credentials',
"scope" => 'req-auth/all',
];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 0,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($data)
));
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($curl);
$errno = curl_errno($curl);
curl_close($curl);
print_r($response);
I tried this code but it is not working on my end How to include 'data-urlencode' using php curl library
(i've seen SOME version of this question so many times, hoping to make a thread with a comprihensive-ish list of answers)
for example, what is the php-curl translation of:
curl -v https://api-m.sandbox.paypal.com/v1/oauth2/token \
-H "Accept: application/json" \
-H "Accept-Language: en_US" \
-u "client_id:secret" \
-d "grant_type=client_credentials"
-v translates to
curl_setopt($ch,CURLOPT_VERBOSE, 1);
PS! by default, curl sends this data to stderr, and stderr is usually visible when running curl from the terminal, but when running php-curl behind a webserver ala nginx/apache, it's not uncommon that stderr is linked to *the web-server's errorlog*, hence the VERBOSE log may arrive in the server error log, rather than the browser. a quickfix to this would be to set a custom CURLOPT_STDERR, ala:
$php_output_handle = fopen("php://output", "wb");
curl_setopt_array($ch, array(
CURLOPT_VERBOSE => 1,
CURLOPT_STDERR => $php_output_handle
));
but because of php garbage collection, when using this quickfix, keep in mind that it will break if php garabge collector closes $php_output_handle before the last curl_exec() call to the same handle.. it's usually not a problem, but it can happen.
.. moving on,
https://api-m.sandbox.paypal.com/v1/oauth2/token translates to:
curl_setopt($ch,CURLOPT_URL, 'https://api-m.sandbox.paypal.com/v1/oauth2/token');
and
-H "Accept: application/json" \
-H "Accept-Language: en_US" \
translates to
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Accept: application/json",
"Accept-Language: en_US"
));
and -u "client_id:secret" translates to:
curl_setopt($ch,CURLOPT_USERPWD, "client_id:secret");
and -d "grant_type=client_credentials" (aka --data) translates to:
curl_setopt_array($ch, array(
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => http_build_query(array(
"grant_type" => "client_credentials"
))
));
hence the full translation is:
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_VERBOSE => 1,
CURLOPT_URL => 'https://api-m.sandbox.paypal.com/v1/oauth2/token',
CURLOPT_HTTPHEADER => array(
"Accept: application/json",
"Accept-Language: en_US"
),
CURLOPT_USERPWD => 'client_id:secret',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => http_build_query(array(
"grant_type" => "client_credentials"
))
));
curl_exec($ch);
what is the translation of curl -F grant_type=client_credentials ?
it's:
curl_setopt_array($ch, array(
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
"grant_type" => "client_credentials"
)
));
what about uploading files, what's the translation of curl -F file=#file/path/to/upload.ext ?
it's:
curl_setopt_array($ch, array(
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
"file" => new CURLFile("filepath/to/upload.ext")
)
));
what's the translation of --location ? it's
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
how to upload JSON? like this:
curl_setopt_array($ch, array(
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json"
),
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => json_encode(array(
"whatever_key" => "whatever_data"
))
));
-X PUT translates to
curl_setopt($ch,CURLOPT_PUT,1);
as for --upload-file , there are several ways to do it,
if you're dealing with small files which easily fits in ram, the easiest way to do it would be:
curl_setopt_array($ch, array(
CURLOPT_PUT => 1,
CURLOPT_POSTFIELDS => file_get_contents($file)
));
but if you need to support big files which you don't want to put in RAM,
$file = "file.ext";
$file_handle = fopen($file,"rb");
$file_size = filesize($file);
curl_setopt_array($ch, array(
CURLOPT_UPLOAD => 1,
CURLOPT_INFILESIZE=>$file_size,
CURLOPT_INFILE=>$file_handle
));
I am working on a Hootsuite script that needs to upload a file using cURL PUT request. On Hootsuite site, I found below example code but I am being able to convert below code in PHP script :
curl -X PUT -H 'Content-Type: video/mp4' \
-H "Content-Length: 8036821" \
-T "/Users/kenh/Downloads/amazing_race.mp4" \
"https://hootsuite-video.s3.amazonaws.com/production/3563111_6923ef29-d2bd-4b2a-a6d2-11295411c988.mp4?AWSAccessKeyId=AKIAIHSDDN2I7V2FDJGA&Expires=1465846288&Signature=3xLFijSn02YIx6AOOjmri5Djkko%3D"
I tried below code but it is still not working :
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://hootsuite-video.s3.amazonaws.com/production/3563111_6923ef29-d2bd-4b2a-a6d2-11295411c988.mp4?AWSAccessKeyId=AKIAIHSDDN2I7V2FDJGA&Expires=1465846288&Signature=3xLFijSn02YIx6AOOjmri5Djkko%3D",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => array('file' => '#' . realpath('/Users/kenh/Downloads/amazing_race.mp4')),
CURLOPT_HTTPHEADER => array(
'Content-Type: video/mp4',
'Content-Length: 8036821'
)
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
So can anybody help me out by telling how to convert above code in PHP ?
I'll be thankful.
Try appending http_build_query on the CURLOPT_POSTFIELDS option, like below :
CURLOPT_POSTFIELDS => http_build_query( array( 'file' => '#' . realpath( '/Users/kenh/Downloads/amazing_race.mp4' ) ) ),
From what I know, the http_build_query is required on the post fields when using PUT method.
I've been trying to use curl to send requests but I keep getting an error:
Error: "" - Code: 3 (The URL was not properly formatted.)
Here is my code that I have been using (after successful authentication and using my access token)
$name = 'test' . rand();
$notes = 'hello hello' . rand();
// Curl request to create asana task.
$url = 'https://app.asana.com/api/1.0/tasks';
$header = "Authorization: Bearer $token";
$curl = curl_init();
curl_setopt($curl, array(
CURLOPT_URL => $url,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
'notes' => $notes,
'name' => $name,
'projects' => 'xxxxxxxxxxxxxxx',
),
));
curl_setopt($curl, CURLOPT_HTTPHEADER, array($header));
$result = curl_exec($curl);
if(!curl_exec($curl)){
die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}
Where I got the project id from the id in the URL of the project.
Update: the equvilant command line curl command I took from https://asana.com/developers/api-reference/tasks and is below. Running this is in the command line worked.
curl -H "Authorization: Bearer tokenid123" https://app.asana.com/api/1.0/tasks -d "notes=test notes" -d "projects[0]=xxxxxxxxxxxxxxx" -d "name=test"
I'm trying for days to upload an image through PHP and OAuth2 to App.net.
Below is the PHP I'm using - it results in this error:
"error_message":"Bad Request: 'type': Required."
<?php
function sendPost()
{
$postData = array(
'type' => 'com.example.upload',
);
$ch = curl_init('https://alpha-api.app.net/stream/0/files');
$headers = array('Authorization: Bearer '.'0123456789',
'Content-Disposition: form-data; name="content"; filename="http://www.example.com/pics/test.jpg";type=image/jpeg',
'Content-Type: image/jpeg',
);
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $postData
));
$response = curl_exec($ch);
}
sendPost();
?>
This is their cURL example from the API documentation:
curl -k -H 'Authorization: BEARER ...' https://alpha-api.app.net/stream/0/files -X POST -F 'type=com.example.test' -F "content=#filename.png;type=image/png" -F "derived_key1=#derived_file1.png;type=image/png" -F "derived_key2=#derived_file2.png;type=image/png;filename=overridden.png"
What type is required and what do I need to change to make it work?
Any feedback is really appreciated. Thank you.
You need to do these following changes
$headers = array(
'Authorization: Bearer '.'0123456789'
);
$postData = array('type' => 'com.example.upload', 'content' => '#/roor/test.png');
Also use this as you are using https
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);