-2

I would like to create a php form that will send a POST using curl.

My curl

curl-H "Content-Type: application/json" \
        -X POST \
        -d '{"Id":"1", "CreatedAt":"2020-07-15 09:00:00","CategoryName":"Kategoria1", \
        "SubcategoryName":"Podkategoria2","LocationCode":"125", \
        "LocationStandCode":"EGZ","ProblemDescription":"Nie włącza się.", \
        "TicketParameters":[{"ParameterCode":"Temat","Value":"Problem"}]}' \
        http://localhost:8080/api/new

php code not working

<?php
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://localhost:8080/ehelpdesk/api/new');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"Id\":\"2\",\"CreatedAt\":\"2020-07-10 11:32:04\",\"CategoryName\":\"Kategoria1\",\"SubcategoryName\":\"\Podkategoria2}");

$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);

?>

Do you have any idea what I'm doing wrong?

TomB
  • 13
  • 2

2 Answers2

0
$ch = curl_init();
 
 curl_setopt($ch, 
 CURLOPT_URL,"http://www.example.com/tester.phtml");
 curl_setopt($ch, CURLOPT_POST, 1);
 curl_setopt($ch, CURLOPT_POSTFIELDS,
        
 "postvar1=value1&postvar2=value2&postvar3=value3");

  // In real life you should use something like:
  // curl_setopt($ch...

// Receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

  $server_output = curl_exec($ch);
  curl_close ($ch);
Eden Moshe
  • 1,097
  • 1
  • 6
  • 16
0

You can handle the curl like this :

<?PHP

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'http://localhost:8080/ehelpdesk/api/new',
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS =>'{
    "Id":"1", 
    "CreatedAt":"2020-07-15 09:00:00",
    "CategoryName":"Kategoria1",
    "SubcategoryName":"Podkategoria2"
}',
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

if you had an array, you could easily convert it to JSON with json_encode (link) for example

<?PHP
$id = 1; 
$date = date('Y-m-d H:i:s');
$arrData = ["Id"=>$id,"CreatedAt"=>$date,"CategoryName"=>"Kategoria1"];
...
//other code
CURLOPT_POSTFIELDS =>json_encode($arrData, JSON_UNESCAPED_UNICODE), 
...

and if you want insert variable directly you can do that like :

<?PHP
$id = 1; 
$date = date('Y-m-d H:i:s');
...
//other code
//Tip: be sure your data has inside double quotation " instead of the single quotation, you can use single quotations inside double quotation and use the variables like this:
CURLOPT_POSTFIELDS =>"{
'Id':{$id}, 
'CreatedAt':{$date},
'CategoryName':'Kategoria1',
'SubcategoryName':'Podkategoria2'
}", 
...

for more information, you should read this PHP cURL article and PHP - concatenate or directly insert variables in string

Milad
  • 328
  • 1
  • 11
  • Thanks for the correct example. And how can I enter the value of some variable instead of e.g. "Id": "1" – TomB Jan 07 '21 at 23:34
  • You're welcome. It would base on your code. If you had an array, you could convert it to JSON with json_encode like this `CURLOPT_POSTFIELDS =>json_encode($yourDataArray),` or if you want to change some variable for example id 1 you can insert the variable directly for example `"Id":$yourIdVariable,` – Milad Jan 08 '21 at 08:11