Send multiple files with same name using PHP CURL - php

Due to the requirements of a project, I need to send two files with the same parameter name as multipart/form-data to an endpoint using PHP CURL.
The parameter name must be files, it cannot be files[] or anything like that, as it would be usual in an endpoint.
For example, the curl request to do this is:
curl --location 'https://example.com/send/830a6665' \
    --header 'Authorization: XXXZZZZZZZXX' \
    --form 'files=#./storage/file1.jpg' \
    --form 'files=#./storage/file2.jpg'
Is it possible to do this in PHP without having to compose the multipart message manually?
Thanks!

Related

VK video API in PHP

I would like an API in PHP for the direct link to download videos from social network VK.
Example:
(VK video link)
$source = 'https://vk.com/video-51189706_456246056';
(Through the source, generate direct links to all available video qualities)
$720p = 'https://psv49-7.daxab.com/cs1-67v4.vkuservideo.net/p18/8835b7e86c59.720.mp4';
$480p = 'https://psv49-7.daxab.com/cs1-67v4.vkuservideo.net/p18/8835b7e86c59.720.mp4';
Is it possible in PHP?
Assuming you have an access token, all you have to do is to perform a POST request at video.get method.
I am not familiar with PHP, but here is the simplest curl representation.
curl -X POST \
-d 'v=5.92' \
-d 'access_token=%YOUR_ACCESS_TOKEN%' \
-d 'videos=-51189706_456246056' \
https://api.vk.com/method/video.get

How to upload file in dropbox using php or curl [duplicate]

I am searching everywhere and haven't been able to locate a suitable example and am not well versed enough to be able to sort it out via the docs. Could someone with more knowledge than I show me how to form the CURL command for OAUTH 2? And is it that I only need the OAUTH 2 secret key? I am being shown an App key, app secret and oauth 2. I am using this in a perl script if it matters.
The closest code I have found is this:
curl --request PUT --header "Content-Length: `ls -la jonathan.txt | awk '{ print $5}'`" --header
"Content-Type: multipart/mixed" --data-binary "#jonathan.txt" "https://api-
content.dropbox.com/1/files_put/dropbox/jonathan.txt?access_token=ABCDEF"
But I don't think that is OAUTH 2?
If you have an access token (created via the app console):
curl -H "Authorization: Bearer <your token>" https://api-content.dropbox.com/1/files_put/auto/ -T <your file path>
You need an access token for the account. (This is typically acquired by going through the OAuth flow, but you can also get one for your own account by clicking the "Generate" button on the page for your Dropbox app. See https://www.dropbox.com/developers/blog/94/generate-an-access-token-for-your-own-account.)
Once you have an access token, your curl command should probably work, though I prefer --header "Authorization:Bearer abc123xyz" over putting the access token in a query parameter. Also, drop the Content-Type: multipart/mixed, since that's not what you're sending.
I'd also recommend "auto" instead of "dropbox," just because it always does the right thing regardless of the app type.
Here is working codes to upload file in dropbox via CURL request.
curl -X POST https://content.dropboxapi.com/2/files/upload \
--header "Authorization: Bearer <your token>" \
--header "Dropbox-API-Arg: {\"path\": \"/file_path.txt\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}" \
--header "Content-Type: application/octet-stream" \
--data-binary "#file_path.txt"

Cloudflare API PHP error

I am trying to add a record to Cloudflare via their API using PHP. For some reason, when I use the same code, the result sometimes gives me success, but at other times, it gives an error saying:
Could not route to /zones/dns_records, perhaps your object identifier is invalid?
and it gives the HTTP response code of 400 and 429. After searching those codes, I learned that the code 429 was something about too many requests. How could I solve this problem?
Unfortunately you are querying the wrong endpoint, the dns_records endpoint is in the format of /zones/:zone_identifier/dns_records
You will need to substitute :zone_identifier with the ID of a given zone, you can get this using the /zones endpoint.
So if you want to add a DNS record using cURL:
curl -X POST "https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a8372d0c353/dns_records" \
-H "X-Auth-Email: user#example.com" \
-H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" \
-H "Content-Type: application/json" \
--data '{"type":"A","name":"example.com","content":"127.0.0.1","ttl":120,"proxied":false}'

PHP and CURL multipart/related

I'm trying to take a picture with a smartphone and send it to a OCR-Server.
I'm now having problem to post the picture with php to the Open-OCR server
Open-OCR provides a sample file howto upload local files to the server.
https://github.com/tleyden/open-ocr/blob/master/docs/upload-local-file.sh
The bash curl command that will be generated looks like this.
curl -v -X POST http://192.168.0.107:9292/ocr-file-upload --header 'Content-Type: multipart/related; boundary="2ac80ee2f500dbe0bae6d3148fe4f960"' --data-binary #-
How can I convert this command to PHP?
I'm really a noob in WEB-Dev and would really appreciate a sample php code that will imitate that command.

Laravel 5 - Post JSON data using curl

I'm trying to POST data using command line using curl.
curl -v POST -d ' { "data1": "sample1", "data2": "sample2" } ' -H "Content-Type: application/json" -H "Authorization: BASIC vkslnkg561mZEqCq3l3RglAOAZ7d8XBeg2VjIAyC" http://localhost/Bowling/public/api/foo
When I try to POST from POSTMAN, I get the data back. But when I use curl, I'm getting a 500 Internal Server Error.
My routes.php
Route::any('api/foo', function () {
return json_encode("Some Data");
});
May I know is it something wrong with my curl statement or some authentication problem that I need to fix in Laravel.
I even tried with
curl -X POST -H "Content-Type: application/json" -d '{"key":"val"}' -H "Authorization: BASIC 0xD8BC2CLksEMh1ScRdG8dWanCDazYiQsGL7sYnl" http://localhost/Bowling/public/api/foo
There is nothing wrong in what you are doing. Its just the delay in your calls which is causing the token mismatch. I faced the same issue while manually testing my APIs via postman. Sad that Laravel doesn't have any decorator to exempt CSRF like Django.
On Postman, I was able to test my APIs issues by writing a route that returns a csrf token. I then copy that token and pass it as form-data in my next POST call. I couldn't find any other alternative.
Let me know if it works for you.

Categories