How to upload a file with PHP curl using HTTP PUT? - php

How can I upload a file with PHP curl, using HTTP PUT instead of POST?
Use case
Assuming the following sample data...
$data = [
'foo' = bar;
'image_file' = curl_file_create('C:\sample.jpg','image/jpg','receipt.jpg')
];
I can post and upload the sample data above with the following curl options...
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
This results in the following data on the receiving end of the post...
Array
(
[foo] => 'bar'
[image_file] => Array
(
[name] => sample.jpg
[type] => image/jpg
[tmp_name] => C:\tmp\php8934.tmp
[error] => 0
[size] => 351836
)
)
Now, I'd like to do the same thing using HTTP PUT.
Attempt 1
If change to an http put using the following option...
//curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
...nothing is posted.
Attempt 2
If I also change the postfields option to...
//curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
The post works, but the upload doesn't. Here's the result...
Array
(
[foo] => 'bar'
[image_file] => Array
(
[name] => C:\sample.jpg
[mime] => image/jpg
[postname] => sample.jpg
)
)
Conclusion
I see no way of uploading files using HTTP PUT. Can it be done?

$image = fopen($file_path, "rb");
$curl = curl_init();
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_PUT, 1);
curl_setopt($curl, CURLOPT_INFILE, $image);
curl_setopt($curl, CURLOPT_INFILESIZE, filesize($file_path));
$result = curl_exec($curl);
curl_close($curl);

It can be done using code below:
#Initiate cURL object
$curl = curl_init();
#Set your URL
curl_setopt($curl, CURLOPT_URL, 'https://local.simbiat.ru');
#Indicate, that you plan to upload a file
curl_setopt($curl, CURLOPT_UPLOAD, true);
#Indicate your protocol
curl_setopt($curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
#Set flags for transfer
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);
#Disable header (optional)
curl_setopt($curl, CURLOPT_HEADER, false);
#Set HTTP method to PUT
curl_setopt($curl, CURLOPT_PUT, 1);
#Indicate the file you want to upload
curl_setopt($curl, CURLOPT_INFILE, fopen('path_to_file', 'rb'));
#Indicate the size of the file (it does not look like this is mandatory, though)
curl_setopt($curl, CURLOPT_INFILESIZE, filesize('path_to_file'));
#Only use below option on TEST environment if you have a self-signed certificate!!! On production this can cause security issues
#curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
#Execute
curl_exec($curl);

Related

VirusTotal Api unclear usage

I scan files from folder ($cfile has fake content intentionally)
$api_key = getenv('VT_API_KEY') ? getenv('VT_API_KEY') :$this->_key;
$cfile = 'asdasdas';
$post = array('apikey' => $api_key,'file'=> $cfile);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.virustotal.com/vtapi/v2/file/scan');
curl_setopt($ch, CURLOPT_POST, True);
curl_setopt($ch, CURLOPT_VERBOSE, 1); // remove this if your not debugging
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); // please compress data
curl_setopt($ch, CURLOPT_USERAGENT, "gzip, My php curl client");
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,True);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result=curl_exec ($ch);
And get fine result with something like :
stdClass Object
(
[scan_id] => ffd8f48291484e1373573bfedf591cdf38565fcc9fc89a4a1d0f16a98da4d4ec-1573916013
[sha1] => d8cc708a1760020e00e1da0e83dc1f7b25e5a5ab
[resource] => ffd8f48291484e1373573bfedf591cdf38565fcc9fc89a4a1d0f16a98da4d4ec
[response_code] => 1
[sha256] => ffd8f48291484e1373573bfedf591cdf38565fcc9fc89a4a1d0f16a98da4d4ec
[permalink] => https://www.virustotal.com/file/ffd8f48291484e1373573bfedf591cdf38565fcc9fc89a4a1d0f16a98da4d4ec/analysis/1573916013/
[md5] => 7ae6fa4e9595a6e9d7987b4cd66f5b7f
[verbose_msg] => Scan request successfully queued, come back later for the report
)
Why do I set file wrong and have good result ?

Get size of remote file from google drive URL

Is there a way to get the size of a remote file https://drive.google.com/uc?export=download&id=54fs54dg45f1f112f2 before downloading the file?
You could make a curl request to get sizeBytes.
<?php
$id = '0ByzJffaEk18uN2ZLeGlIRGVOaDJmWS1WU1RUN3d***';
$ch = curl_init('https://drive.google.com/uc?id='.$id.'&export=download');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, []);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
$object = json_decode(str_replace(')]}\'', '', $result));
print_r($object);
Result:
stdClass Object
(
[disposition] => SCAN_CLEAN
[downloadUrl] => https://doc-08-8o-docs.googleusercontent.com/docs/...
[fileName] => filename.sh
[scanResult] => OK
[sizeBytes] => 4936
)

how to delete photo post id facebook by grab api php with curl

i try to delete photos i have uploaded with app by php
the result of upload example :
{"id":"429774393794352","post_id":"276744849097308_429774413794350"}
first which one id or post_id will be use
and how to select it with php ??
and how to make order to delete it with php code by curl library
my idea is , i will store all photos ids into sql to delete it any time
my upload code i used
$file='./'.$new_string;
$args = array(
'message' => $message,
);
$args[basename($file)] = '#' . realpath($file);
$ch = curl_init();
$url = 'https://graph.facebook.com/album id /photos?access_token='.$accsesss;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$a1=$data = curl_exec($ch);
print_r(json_decode($data,true));
echo'<br/>';
i try
$args = array(
'id' => '276744849097308_429774413794350',
'access_token' => $accsesss ,
);
$ch = curl_init();
$url = 'https://graph.facebook.com/276744849097308_429774413794350?method=DELETE&access_token=$accsesss';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true); $a1=$data = curl_exec($ch);
print_r(json_decode($data,true));
echo'<br/>';
result
Array ( [error] => Array ( [message] => Invalid OAuth access token. [type] => OAuthException [code] => 190 ) )
$Curl_Session = curl_init('https://graph.facebook.com/429774393794352');
curl_setopt ($Curl_Session, CURLOPT_POST, 1);
curl_setopt ($Curl_Session, CURLOPT_POSTFIELDS, "method=DELETE&access_token=$masteracsess");
curl_setopt ($Curl_Session, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($Curl_Session, CURLOPT_RETURNTRANSFER, 1);
echo $a8=curl_exec ($Curl_Session);
curl_close ($Curl_Session);
thanks for your help #CBroe

Make 2 cURL requets with $_POST

I am trying to do 2 requets in cURL. The first request is login me in, and I conserve a txt file to make the second requests when still loged.
The first part work like a charm, the second part doesn't work.
There is my code.
$lien = 'https://mysite.com/login';
$postfields = array(
'username' => 'test',
'password' => 'test'
);
$path_cookie = 'connexion.txt';
if (!file_exists(realpath($path_cookie))) touch($path_cookie);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $lien);
curl_setopt($curl, CURLOPT_COOKIESESSION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($curl, CURLOPT_COOKIEJAR, realpath($path_cookie));
$return = curl_exec($curl);
echo($return);
curl_close($curl);
$lien2 = 'https://mysite.com/myform';
$postfields2 = array(
'data1' => 'test123',
'data2' => 'Account',
'sort' => '3',
'fileFormat' => '0',
'timezone' => 'Eastern+Standard+Time',
'zeroRated' => 'true',
'startDate' => '2013-05-01',
'endDate' => '2013-05-31'
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $lien2);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postfields2);
curl_setopt($curl, CURLOPT_COOKIEFILE, realpath($path_cookie));
$return = curl_exec($curl);
echo $return;
curl_close($curl);
unlink(realpath($path_cookie));
I am using the second part to $_post data to execute a form. The form processing takes place in the same page...
My connexion still on, but I don't know, the server is shooting me an error like :
An exception occurred while processing your request. We recorded the exception..
It doesn't help me. Any one have an idea?
Thanks.
You have maybe a problem with SSL at this point.
Try to add these options:
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
finally I receive an answer from the support team. The "Eastern+Standard+Time" should be "Eastern Standard Time", the server will automaticly escape those string ... Thaks to every body for your help !

cURL post not working PHP

I'm having trouble using cURL for a specific page.
A live code working: http://svgen.com/jupiter.php
Here is my code:
$url = 'https://uspdigital.usp.br/jupiterweb/autenticar';
$data = array('codpes' => 'someLogin', 'senusu' => 'somePass', 'Submit' => '1');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIEJAR, "FileHere");
curl_setopt($ch, CURLOPT_COOKIEFILE, "FileHere");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
curl_exec($ch);
curl_close($ch);
Although I had used the same url and post data, file_get_contents worked:
$options = array('http' => array('method' => 'POST','content' => http_build_query($data)));
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);
Someone could help me?
Thanks.
Most probably it is the SSL verification problem.
Add
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
Also if you use CURLOPT_PROTOCOLS option, it should be HTTPS since you are posting to a secure url
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS); // you currently have http
$data = array('codpes' => 'someLogin', 'senusu' => 'somePass', 'Submit' => '1');
should have been
$data = http_build_query(array('codpes' => 'someLogin', 'senusu' => 'somePass', 'Submit' => '1'));
It automatically url encodes your query string as well and is safer than manual methods..
make your post data as:
$data = array('codpes' => 'someLogin', 'senusu' => 'somePass', 'Submit' => '1');
$postData = "";
foreach( $data as $key => $val ) {
$postData .=$key."=".$val."&";
}
$postData = rtrim($postData, "&");
and change:
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
to
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
Try these:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_CAINFO, '/etc/pki/tls/cert.pem'); // path is valid for RHEL/CentOS
This makes sure the resource you're "curling" has a valid SSL certificate. It is not recommended to set "CURLOPT_SSL_VERIFYPEER" to false (0).
You're on a secure connection, why are you using :
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
Use instead :
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);

Categories