-1

I want to POST the following JSON HTTP API request in PHP

    curl -d '{"text":"Hello, #param#.","port":[2],"param":
    [{"number":"123456","text_param ":["John"],"user_id":1}]}’-u 
    myusername:mypassword –H "Content-Type: application/json"  
    http://accbd.com/api/send_req 
zimmerrol
  • 4,872
  • 3
  • 22
  • 41
Accbd
  • 1
  • 1

1 Answers1

0

You can try below code

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://accbd.com/api/send_req');
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type' => 'application/json'));
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_USERPWD, "myusername:mypassword");  
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, '{"text":"Hello, #param#.","port":[2],"param":[{"number":"123456","text_param ":["John"],"user_id":1}]}');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);


$data = curl_exec($curl);
curl_close($curl);
echo $data;exit;
Lalmani Dewangan
  • 306
  • 2
  • 11
  • got error Pragma: no-cache Cache-Control: no-cache Content-Type: application/json – Accbd Dec 24 '17 at 16:56
  • What error you are getting? – Lalmani Dewangan Dec 24 '17 at 17:00
  • HTTP/1.1 400 Page not found Server: Web Server/2.1.0 Date: Sat Dec 23 10:07:17 2017 Pragma: no-cache Cache-Control: no-cache Content-Type: application/json – Accbd Dec 24 '17 at 17:08
  • curl_setopt($curl, CURLOPT_URL, 'http://192.168.11.1:5851/api/send_sms'); curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type' => 'application/json')); curl_setopt($curl, CURLOPT_HEADER, 1); curl_setopt($curl, CURLOPT_USERPWD, "ali:reza345934"); curl_setopt($curl, CURLOPT_TIMEOUT, 30); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, '{"text":"Hello, #param#.","port":[2,3],"param":[{"number":"01784787670","text_param ":["John"],"user_id":1},{"number":"01784787670", "text_param":["Sam"],"user_id":2}]}'); curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); – Accbd Dec 24 '17 at 17:10
  • Please use correct URL for CURLOPT_URL option. http://192.168.11.1:5851/api/send_sms seems not working. – Lalmani Dewangan Dec 24 '17 at 17:15
  • If I add port then I got this error but if I not add port then got blank page. Noting happened – Accbd Dec 24 '17 at 17:27