James Murphy
asked on
PHP Curl Request - please help!
Hi,
I am trying to make a submission via PHP Curl.
I need to submit a csv file as part of the body (using a bearer token for security)
I am having issues getting it to submit being told that my data has to be an array, -= Could anyone please advise where I am going wrong?
I did have an issue where I was getting a 0 status, returned, but once I encoded the variable $fielddata - I am now getting back an error from the API basically saying that I need to provide data in an array. The only thing I can think of is the curl_file might be wrong?
basically I am just trying to submit the file that is in the same directory as the php file I am running.
I am trying to make a submission via PHP Curl.
I need to submit a csv file as part of the body (using a bearer token for security)
I am having issues getting it to submit being told that my data has to be an array, -= Could anyone please advise where I am going wrong?
I did have an issue where I was getting a 0 status, returned, but once I encoded the variable $fielddata - I am now getting back an error from the API basically saying that I need to provide data in an array. The only thing I can think of is the curl_file might be wrong?
basically I am just trying to submit the file that is in the same directory as the php file I am running.
$template = "Hi {{name}}! Click here {{url}} to enter our portal({{amount}}).";
$cfile = new CURLFile('meekimoney.csv','text/csv','meekimoney');
//$cfile = curl_file_create('meekimoney.csv','text/csv','meekimoney.csv');
// Assign POST data
echo "<br><br>";
$fielddata = array('file' => $cfile, 'name' => $nameofcampaign, 'template' => $template );
$data_json = json_encode($fielddata);
$headers = array();
$headers[] = "Authorization: Bearer ".$Bearer;
//$headers[] = "Cache-Control: no-cache";
//$headers[] = "Postman-Token: 202fee3b-9efd-48fe-9d98-483b7f9ebb64";
$headers[] = "Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW";
//$headers[] = "Content-Type: application/json; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW";
//boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW";
$ch = curl_init();
$url = "http://api.stage.ubiquityplatform.com/meekimoney/batch/v1/invoiceBatch.json";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//curl_setopt($ch, CURLOPT_POSTFIELDS,$fielddata);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'post');
$response = curl_exec($ch);
//$response = curl_exec($curl);
$err = curl_error($curl);
echo curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo "This is the response: ".$response;
}
}
echo "<br><br>";
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you data is in fact an array then all you need to do is json_encode($data) to turn it into a postable string.
If you data is in fact an array then all you need to do is json_encode($data) to turn it into a postable string.Mark, json encoding alone would still cause issues with certain characters that are used in urls e.g. space, &, =, #, :, /
Oh my bad! Thanks for pointing that out. I meant to type urlencode(json_encode($dat a));
Then on the receiving server it would be: json_decode(urldecode($_PO ST['data'] )); // or whatever your url variable for data is.
Then on the receiving server it would be: json_decode(urldecode($_PO
ASKER
thank you very much both - very much appreciated!!