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
3
  • 3
    Did you run this code? Short of improper strings, you're on the right track. Commented Jul 22, 2013 at 14:41
  • I did, just to be sure. It isn't accessing the url, while my terminal command is working (which I added to question). Commented Jul 22, 2013 at 14:44
  • Well, $myjson is not set correct... change the first and last " for ' ;) (quote it correctly!) Commented Jul 22, 2013 at 14:46

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for the help. I can't believe I forgot that! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.