4

I've found at the StackOverflow answer here exactly how I want to post data to another web url.

However, I want this command to be executed in my php web script, not from the terminal. From looking at the curl documentation, I think it should be something along the lines of:

<?php 

$url = "http://myurl.com";
$myjson = "{\"column1\":\"1\",\"colum2\":\"2\",\"column3\":\"3\",\"column4\":\"4\",\"column5\":\"5\",\"column6\":\"6\"}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $myjson);
curl_close($ch);
?>

What is the best way to do that?

The current terminal command that is working is:

curl -X POST -H "Content-Type: application/json" -d '{"column1":"1","colum2":"2","column3":"3","column4":"4","column5":"5","column6":"6"}' https://myurl.com
Robert Pounder
  • 1,490
  • 1
  • 14
  • 29
AllieCat
  • 3,890
  • 10
  • 31
  • 45

1 Answers1

5

There are two problems:

  1. You need to properly quote $myjson. Read the difference between single and double quoted strings in PHP.
  2. You are not sending the curl request: curl_exec()

This should move you in the right direction:

<?php
$url = 'http://myurl.com';
$myjson = '{"column1":"1","colum2":"2","column3":"3","column4":"4","column5":"5","column6":"6"}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $myjson);
$result = curl_exec($ch);
curl_close($ch);
Jason McCreary
  • 71,546
  • 23
  • 135
  • 174