简体   繁体   中英

send form data to API by curl

I would like to send a form data to API this API has a key and profile must be provided then after sending data by post request i have to send get request to get the info back

this is the post function they provide :

curl -k -X POST --header "X-API-KEY:TOKEN" -F "profile=B2B" -F
"data_file=@/var/tmp/testfile.csv" -F "md5=ae0eca4f5671cbdc19340a1df2567611"
https://api.cloudHygiene.com/task/run

and this is the get function they provide :

curl -k -X GET --header "X-API-KEY:TOKEN"
https://api.cloudhygiene.com/task/get_file? 
package_name=hWviW2hLui_1477652942&f
ilename=_report.xls

Are you asking how to run the cURL command in PHP? If so here is the first part of the cURL request in PHP:

// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://api.cloudHygiene.com/task/run");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);

$headers = array();
$headers[] = "X-Api-Key: TOKEN";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close ($ch);

Here is the second part:

// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://api.cloudhygiene.com/task/get_file?");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");


$headers = array();
$headers[] = "X-Api-Key: TOKEN";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close ($ch);

Again, I am only speculating you are asking to run the cURL request in PHP. If not, I suggest rewording your question and providing more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM