send form data to API by curl - php

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.

Related

Using cURL GET statement in PHP

I have a cURL GET statement:
curl -v -X GET "https://admiraltyapi.azure-api.net/uktidalapi/api/V1/Stations/{stationId}" -H "Ocp-Apim-Subscription-Key: {subscription key}" --data-ascii "{body}"
that works fine in a terminal window (with my sub key) - I now want to use it in a PHP script to retrieve the associated JSON. Code suggestions for achieving this appreciated...
Not tested but you need something like this with PHP CURL.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://admiraltyapi.azure-api.net/uktidalapi/api/V1/Stations/{stationId}');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_TRANSFERTEXT, true);
$headers = array();
$headers[] = 'Ocp-Apim-Subscription-Key: {subscription key}';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);

Coverting CURL command to CURL php

I am trying to add documents to elastic search index via bulk API. My CURL query works fine on command line and I converted it to PHP.
When I am trying to run PHP code, nothing happens. Neither documents are added in index in elastic search, nor any error appears.
CURL Query:
curl -H "Content-Type:application/json" -XPOST "http://localhost:9200/inven/default/_bulk?pretty" --data-binary "#file_name.json"
PHP Query:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost:9200/inven/default/_bulk?pretty');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$post = array(
'file' => '#' .realpath('file_name.json')
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
I have set ini_set('max_execution_time', 0); as well, in case it, it was timing out.
What could be the issue?
You are not sending the actual file contents, just its filename as a string. Try this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost:9200/inven/default/_bulk?pretty');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1 );
// The line below assumes the file_name.json is located in the same directory
// with this script, if not change the path to the file with the correct one.
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents('file_name.json') );
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
try $post = '#' .realpath('file_name.json')
If you are using PHP 5.5+, use:
$post = [
'file' => curl_file_create('file_name.json')
];
For more info check: Fix CURL file uploads.

PHP Curl request fails but works in other clients

I have a very simple API request that sends a JSON encoded string with a userID and password. When I try to execute the command through my Chrome Extension - Advanced Request Client or through Swagger it works perfect, but the exact same request in CURL is failing, the response is returning invalid password.
The code should be really straight forward, is there some setting in CURL I'm missing?
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost:8080/api");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"userid\":\"1231\",\"password\":\"123\"}");
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Content-Type: application/json";
$headers[] = "Accept: application/json";
$headers[] = "X-Databasename: x";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);

Sending a put command with Curl on php

Given the following shell command:
curl -X PUT -d arg1='value1'-d arg2='value2' -d arg3='value3' \
https://api.myurl/1/subscriptions/1234;
What is the PHP Curl equivalent?
Checkout this site: https://incarnate.github.io/curl-to-php/
Created this:
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "arg2='value2'");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "arg1='value1'-d&arg3='value3'");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);

convert Curl to php

Is it possible to convert this curl command to PHP?
curl -H "cwauth-token: CwEqP_oQXCiDKt3ph0leZ2AlKsqE6E4N " "https://reports.voluum.com/report?from=2014-09-24T00%3A00%3A00Z&to=2014-0925T00%3A00%3A00Z&tz=Europe%2FWarsaw&sort=visits&direction=desc&columns=campa ignName&columns=visits&columns=clicks&columns=conversions&columns=revenue&co lumns=cost&columns=profit&columns=cpv&columns=ctr&columns=cr&columns=cv&colu mns=roi&columns=epv&columns=epc&columns=ap&columns=errors&groupBy=campaign&o ffset=0&limit=100&include=active"
Here's the fish:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://reports.voluum.com/report?from=2014-09-24T00%3A00%3A00Z&to=2014-0925T00%3A00%3A00Z&tz=Europe%2FWarsaw&sort=visits&direction=desc&columns=campa ignName&columns=visits&columns=clicks&columns=conversions&columns=revenue&co lumns=cost&columns=profit&columns=cpv&columns=ctr&columns=cr&columns=cv&colu mns=roi&columns=epv&columns=epc&columns=ap&columns=errors&groupBy=campaign&o ffset=0&limit=100&include=active");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$headers = array();
$headers[] = "Cwauth-Token: CwEqP_oQXCiDKt3ph0leZ2AlKsqE6E4N";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
Here's how to fish:
https://curl.haxx.se/docs/manpage.html
https://secure.php.net/manual/en/function.curl-setopt.php
And here's a few fishing machines you can use:
http://curl.trillworks.com/#php
https://incarnate.github.io/curl-to-php/
And BTW, here's why you should consider fishing alternatives:
Requests for PHP
Guzzle, PHP HTTP Client

Categories