-3

I have to execute the following via php script

curl --location --request POST 'https://api.mydomain.in/api/comm/wa/send/text' \
--header 'Content-Type: application/json' \
--header 'token: xyz123456' \
--data-raw '{
   "phone": "8822992929",
   "ID": 26,
   "text": "Dear Customer. Thank you for your purchase. "

}'

How do I do this via php curl exec. I do not see how I can pass the data in this

charak
  • 187
  • 3
  • 15
  • 1
    Can you please share what you've tried and what exact problems you ran into? – El_Vanja Apr 20 '21 at 09:57
  • I'd suggest starting from reading [the docs](https://www.php.net/manual/en/book.curl.php). – biesior Apr 20 '21 at 10:01
  • I think if asked more clearly, this would probably have been a duplicate of https://stackoverflow.com/questions/871431/raw-post-using-curl-in-php – IMSoP Apr 20 '21 at 10:06

2 Answers2

1

You should try to do something from your side and mention it on the question. Anyways here's the solution for your problem

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://api.mydomain.in/api/comm/wa/send/text',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS =>'{
   "phone": "8822992929",
   "ID": 26,
   "text": "Dear Customer. Thank you for your purchase. "

}',
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json',
    'token: xyz123456'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;
localroot
  • 566
  • 3
  • 13
0
$post = array( "phone" => "8822992929",
"ID" => 26,
"text" => "Dear Customer. Thank you for your purchase.");
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
/*$headers = array(
    'Content-Length: 0',
    'Content-Type: application/json; charset=us-ascii',
    'token: xyz123456',
);*/
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
//curl_setopt($ch, CURLOPT_USERNAME, $username);
//curl_setopt($ch, CURLOPT_USERPWD, $password);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);

It's a version I use for my php. You need to edit the $headers array to use json and also send the token. It's hard to tell, but I think you need data-raw as "POST-like" array, which then goes to the CURLOPT_POSTFIELDS, $post part. Username, password, are optional, you may need to change authentication too.

kry
  • 362
  • 3
  • 13