Basic Curl to PHP Conversion - php

I'm trying to figure out how to do this CURL command:
curl https://uploads.stripe.com/v1/files \
-u sk_test_OCHANGEDPcDr0: \
-F purpose=identity_document \
-F file="#/path/to/a/file.jpg"
In PHP, I'm using this PHP script:
https://github.com/php-mod/curl
And tried this:
$curl->setBasicAuthentication('-u', 'sk_live_CHANGED');
$curl->post('https://uploads.stripe.com/v1/files', array(
'purpose' => 'dispute_evidence',
'file' => $targetFile,
));
But I think the syntax is completely wrong, I'm really new to curl and don't understand what all the different -u and -F commands PHP equivalents are.

The -u username:password option translates to:
$curl->setBasicAuthentication('username', 'password');
To upload a file, you need to put # before the filename, just like in the curl command.
$curl->post('https://uploads.stripe.com/v1/files', array(
'purpose' => 'dispute_evidence',
'file' => '#' . $targetFile,
));

You'll probably want to try something like this:
$headers = array(
'Authorization: Bearer ' . $your_api_key,
'Content-Type: multipart/form-data'
);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://uploads.stripe.com/v1/files',
CURLOPT_POST => true,
CURLOPT_INFILESIZE => '10000',
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => array(
'purpose' => 'dispute_evidence',
'file' => '#' . realpath($path_to_file)
)
));
$resp = curl_exec($curl);
curl_close($curl);
EDIT: Alternatively, you can upload files directly from the browser like this.

If you have a functional curl command invocation, you can get what you are after by adding the --libcurl filename.c option.
This will generate a C program to do what your command line did. Though you are after PHP and not C, this is still useful because they both use the same names for various options.

Related

How to translate curl arguments to php-curl?

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

How to write curl -X PUT -T script in php

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.

How to write Curl api example into PHP curl

I am using https://apptweak.io api,
they didn't mention PHP example in their code example...
please tell me, how to understand and write Curl API example into PHP curl
Here is simple "appteak.com" curl example
curl -G https://api.apptweak.com/ios/applications/284993459/informations.json \
-d country=us \
-d language=en \
-d device=iphone \
-H "X-Apptweak-Key: my_api_key"
Here is other example
$ curl -H "X-Apptweak-Key: my_api_key" \
https://api.apptweak.com/android/applications/com.facebook.katana.json?country=be&language=nl
How to write these curl in PHP?
i did this in php, after google, but its not work for me.
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://api.apptweak.com/android/applications/com.facebook.katana.json?country=be&language=nl',
// CURLOPT_USERAGENT => 'Codular Sample cURL Request',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
'X-Apptweak-Key' => "my_api_key",
)
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
var_dump($resp ); //return false
I find correct answer
$token = "my_api_key";
$url = "http://URL.com";
$options = array('http' => array(
'header' => array("X-Apptweak-Key: $token"),
));
$context = stream_context_create($options);
$result = file_get_contents($url, 0, $context);

PHP cURL giving a different result than native cURL

When I run the following cURL call natively I get an expected result.
curl -X POST -H "Accept:application/json" -H "Content-Type: application/x-www-form-urlencoded" "https://XXXX" -d 'grant_type=authorization_code&code=YYYY
When I run the cURL call via PHP I get a different response.
$curl = curl_init('https://XXXX');
curl_setopt_array($curl, array(
CURLOPT_HTTPHEADER => array(
'Accept: application/json',
'Content-type: application/x-www-form-urlencoded'
),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => array(
'grant_type' => 'authorization_code',
'code' => 'YYYY'
),
));
$response = curl_exec($curl);
curl_close($curl);
Is there any functional difference between the native call and the PHP approach? Thanks.
The problem is that PHP's cURL doesn't trust the server's HTTPS certificate. Differently command line curl by default does.
The quick fix is to configure cURL to always trust the server:
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
The proper fix is described here
http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/

Sending a POST with PHP doesn't work with curl or file_get_contents, just normal bash CURL

I'm having some serious problems sending a POST. Using curl on the shell, my POST works perfectly. However, when using PHP curl, or file_get_contents, it doesn't work at all. I get a 500 error from the webserver.
curl -X POST -H"Content-Type:application/xml" "http://myserver:8080/createItem?name=NewItem" --user root:123456 --data-binary #template.xml
And this:
$options = array(
CURLOPT_HEADER => 1,
CURLOPT_HTTPHEADER => array("Content-Type:application/xml"),
CURLOPT_URL => "http://myserver:8080/createItem?name=" . rawurlencode("NewItem"),
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FORBID_REUSE => 1,
CURLOPT_TIMEOUT => 20,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => "root:123456",
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => file_get_contents("template.xml"),
);
$post = curl_init();
curl_setopt_array($post, $options);
if(!$result = curl_exec($post)) {
trigger_error(curl_error($post));
}
curl_close($post);
And this:
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => sprintf("Authorization: Basic %s\r\n", base64_encode('root:123456')) . "Content-Type:application/xml",
'timeout' => 20,
'content' => file_get_contents("template.xml"),
),
));
$ret = file_get_contents("http://myserver:8080/createItem?name=" . rawurlencode("NewItem"), false, $context);
Am i doing something absurd here and i'm not seeing? I don't see a reason for the normal curl from the shell to work perfectly, but not the PHP implementations.
Hum... I don't think that's possible with php/curl, but try:
CURLOPT_POSTFIELDS => array('#template.xml'),
It IS possible, take a look at this:
// same as <input type="file" name="file_box">
$post = array(
"file_box"=>"#/path/to/myfile.jpg",
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
Source: http://dtbaker.com.au/random-bits/uploading-a-file-using-curl-in-php.html
According to the documentation, headers here
'header' => sprintf("Authorization: Basic %s\r\n", base64_encode('root:123456'))
. "Content-Type:application/xml",
should end with \r\n [see example 1 here]
'header' => sprintf( "Authorization: Basic %s\r\n", base64_encode('root:123456'))
. "Content-Type: application/xml\r\n",

Categories