I'm attempting to download file from dropbox to my server, nothing is returned but i know the file exists
$headers = array('Authorization: Bearer '.$access_token,
'Content-Type: application/json',
'Dropbox-API-Arg: {"path": "'.$filepath.'"}');
$file = fopen($filename, "w+");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://content.dropboxapi.com/2/files/download");
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FILE, $file);
$result = curl_exec($ch);
curl_close ($ch);
fclose($file);
Where did i go wrong? How do i solve?
try this way and check the output of the result. and save it to the local server.
$url = 'https://content.dropboxapi.com/2/files/download';
$formData = [
"path"=> $filepath
];
$ch = curl_init($url);
$authorization = "Authorization: Bearer ".$access_token;
$request_data = "Dropbox-API-Arg: ".json_encode($formData);
curl_setopt($ch, CURLOPT_HTTPHEADER, array($authorization, $request_data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch);
curl_close($ch);
dd($result)
Related
I would like to upload a textfile to Google Cloud Storage.
I use this function in php:
function upload_object_to_Google_bucket($bucket,$accesstoken,$proxy,$objectname,$objectpath)
{
#Build the CUrl: incarnate.github.io/curl-to-php
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, "https://storage.googleapis.com/upload/storage/v1/b/".$bucket."/o?uploadType=media&name=".$objectname);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
# curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, false);
$file=$objectpath.$objectname;
$item=make_curl_file($file);
print_r($item);
$file = $objectpath.$objectname;
$post = array(
$item
);
# curl_setopt($ch, CURLOPT_UPLOAD, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
# curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
# curl_setopt($ch, CURLOPT_NOBODY, true);
$headers = array();
$headers[] = 'Authorization: Bearer '.$accesstoken;
$headers[] = 'Content-Type: '.object_to_array($item)["mime"];
$headers[] = 'Content-Length: '.filesize($file);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
print_r($response);
if (curl_errno($ch)){
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
}
It's all fine, but the textfile has become new content in the Bucket.
--------------------------9ad622dfd933f230
Content-Disposition: form-data; name="0"; filename="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xml"
Content-Type: text/plain
These lines are added to the file. Does anyone has an idea whats the problem and how I can avoid this behaviour?
What would be the php curl version of the below command
curl -X PUT -H 'Content-Type: text/csv' -H 'Accept: application/json' -d #file.csv [URL]
I have the below php curl version which doesn't work
$headers = [
'Content-Type: text/csv',
'Accept: application/json'
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($filename));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
Try below code
$headers = [
'Content-Type: text/csv',
'Accept: application/json'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ''.$url'');
curl_setopt($ch, CURLOPT_PUT, 1);
$fp = fopen($file_path, 'r');
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file_path));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response= curl_exec ($ch);
fclose($fp);
There is a tool to convert cURL command to PHP version of cURL. You can try this website: curl-toPHP.
This is the example output using your cURL command.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'your-post-remote');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$post = array(
'file' => '#' .realpath('file.csv')
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
$headers = array();
$headers[] = 'Content-Type: text/csv';
$headers[] = 'Accept: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
follow codes is php code ,but it don't download anything.
$postdata=$jsonstr;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
//'Content-Type: application/x-www-form-urlencoded"',
'Content-Length: ' . strlen($postdata)
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata );
$output = curl_exec($ch);
$info = curl_getinfo($ch);
echo "<br/>-------------------<br/>";
//print_r($output);
echo "<br/>-------------------<br/>";
mySaveFile($output, "./file.tmp");
curl_close($ch);
I get 'file.tmp' always is size 0.I suspect I set up a mistake for CURLOPT_HTTPHEADER,
thanks for everyone help.
We are trying to convert use the following curl cmdline which is working fine
curl --request POST --data-binary #"/home/project/enrol/my.wav" --header "Content-Type:audio/wav" --header "VsitEmail: 200037test2#test.com" --header "VsitPassword: 8270f2824111e04d9278c01a92b388147d9d02e0b50d946d25d00db375ff1282" --header "VsitDeveloperId: 200037" https://siv.voiceprintportal.com/sivservice/api/enrollments
to PHP libcurl which is constantly giving internal error.
Please suggest where i am wrong .
Code
$ch = curl_init();
$path="/home/project/enrol/my.wav";
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: audio/wav","VsitEmail: 200037test2#test.com","VsitPassword: 8270f2824111e04d9278c01a92b388147d9d02e0b50d946d25d00db375ff1282","VsitDeveloperId: 200037"));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt( $ch , CURLOPT_VERBOSE , 0 );
curl_setopt($ch, CURLOPT_POSTFIELDS,array('file' => '#'.$path));
$result = curl_exec($ch);
//close connection
curl_close($ch);
print_r($result);
Update 1
$localfile='/home/project/enrol/my.wav';
$url = 'https://siv.voiceprintportal.com/sivservice/api/enrollments';
$ch = curl_init();
$fields = array('file' => '#' .$localfile);
$resource = curl_init();
curl_setopt($resource, CURLOPT_URL, $url);
//curl_setopt($resource, CURLOPT_HTTPHEADER, $header);
curl_setopt($resource, CURLOPT_HTTPHEADER, array("Content-Type:
audio/wav","VsitEmail: 200037test2#test.com","VsitPassword: 8270f2824111e04d9278c01a92b388147d9d02e0b50d946d25d00db375ff1282","VsitDeveloperId: 200037"));
curl_setopt($resource, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($resource, CURLOPT_POST, 1);
curl_setopt($resource, CURLOPT_BINARYTRANSFER, true);
curl_setopt($resource, CURLOPT_POSTFIELDS, $fields);
//$result = json_decode(curl_exec($resource));
$result=curl_exec($resource);
curl_close($resource);
echo "The result is";
print_r($result);
Check the following code
$url = 'http://google.com';
$header = array('Content-Type: multipart/form-data');
$fields = array('file' => '#' . $_FILES['file']['tmp_name'][0]);
$resource = curl_init();
curl_setopt($resource, CURLOPT_URL, $url);
curl_setopt($resource, CURLOPT_HTTPHEADER, $header);
curl_setopt($resource, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($resource, CURLOPT_POST, 1);
curl_setopt($resource, CURLOPT_POSTFIELDS, $fields);
$result = curl_exec($resource);
curl_close($resource);
It got resolved by passing the raw data file instead of an array. Correct code is
$localfile='#/home/project/enrol/my.wav';
$url = 'https://siv.voiceprintportal.com/sivservice/api/enrollments';
$data = file_get_contents('/home/project/enrol/my.wav');
$headers = array();
$headers[] = 'X-Requested-With: JSONHttpRequest';
$headers[] = 'Content-Type: audio/wav';
$headers[] = 'VsitEmail: 200037test2#test.com';
$headers[] = 'VsitPassword: 8270f2824111e04d9278c01a92b388147d9d02e0b50d946d25d00db375ff1282';
$headers[] = 'VsitDeveloperId: 200037';
$resource = curl_init();
curl_setopt($resource, CURLOPT_URL, $url);
curl_setopt($resource, CURLOPT_HTTPHEADER,$headers);
curl_setopt($resource, CURLOPT_POST,1);
curl_setopt($resource, CURLOPT_BINARYTRANSFER, true);
curl_setopt($resource, CURLOPT_POSTFIELDS,$data);
$result = json_decode(curl_exec($resource));
curl_close($resource);
echo "The result is";
print_r($result);
Hi all! I have curl command (audio file upload):
curl -k -v -H "Expect: " -H "Content-Type:application/octet-stream" --data-binary '#/Path/To/File/test.wav' -X POST 'https://myapi.com?param1=xxx¶m2=yyy'
File successfully uploaded and readable. But when I used php script:
$filename = 'test.wav';
$file = '#/Path/To/File/test.wav';
$post_url = "https://someapi.com?param1=xxx¶m2=yyy";
$post_str = array("$filename" => $file);
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/octet-stream'));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect: '));
curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$http_body = curl_exec($ch);
var_dump($http_body);
File successfully uploaded and test.wav is not valid (with some error). What am I doing wrong in the script?
I suspect the issue is these lines:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/octet-stream'));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect: '));
The second call will remove the "Content-Type" Header. Try consolidating these:
$headers = array('Content-Type:application/octet-stream','Expect: ');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$filename = 'test.wav';
$file = '/Path/To/File/test.wav';
$post_url = "https://someapi.com?param1=xxx¶m2=yyy";
$post_str = file_get_contents($file);
$headers = array('Content-Type:application/octet-stream', 'Expect: ');
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch,CURLOPT_VERBOSE,true);
curl_setopt($ch, CURLOPT_STDERR, fopen("/Path/to/header.txt", "w+"));
$http_body = curl_exec($ch);
It's work perfect. I solved the problem myself. File upload to the server binary. Thank you all!