Link to home
Start Free TrialLog in
Avatar of James Murphy
James MurphyFlag for Australia

asked on

Convert Curl Snippet from Postman to snippet I can use properly.

Hi,

Could someone please help me.

What i need to do, is convert the below to a proper snippet I can use in my php to do the curl post and post up different field values as required.  I exported this from postman.


your help is very much appreciated!!
<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "http://someapi.com",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"template\"\r\n\r\nHi {{name}}! Click here {{url}} to pay your outstanding account.\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"; filename=\"C:\\xampp\\htdocs\\test.csv\"\r\nContent-Type: text/csv\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"name\"\r\n\r\nTESTUPLOAD-1\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
  CURLOPT_HTTPHEADER => array(
    "Authorization: Bearer XYZ",
    "Cache-Control: no-cache",
    "Postman-Token: 137a61fa-24dc-493e-9686-5e455d4d8a78",
    "content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

Open in new window

Avatar of Rob
Rob
Flag of Australia image

That looks like it should work and it is PHP code... what exactly is the issue you're having?
Avatar of James Murphy

ASKER

Hi,

Yes it does work, but i need to be able to feed variables including files into it, and everything I have tried seems to be incorrect.
I was hoping to try and get it similar to the below:

$ch = curl_init("http://www.example.com/");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);

Open in new window


rather than the current method.

any help is very much appreciated!
Basically I need to be able to feed a file number in there - when I attempt to post it is acting as though it cannot find the file required to be posted up.

CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"template\"\r\n\r\nHi {{name}}! Click here {{url}} to pay your outstanding account ({{amount}}).\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"; filename=\"C:\\xampp\\htdocs\\test\\test.csv\"\r\nContent-Type: text/csv\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"name\"\r\n\r\n".$campaignname."-1\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Rob
Rob
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
many thanks!