Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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
share|improve this question
3  
Did you run this code? Short of improper strings, you're on the right track. –  Jason McCreary Jul 22 '13 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). –  AllieCat Jul 22 '13 at 14:44
    
Well, $myjson is not set correct... change the first and last " for ' ;) (quote it correctly!) –  Eleazan Jul 22 '13 at 14:46
add comment

1 Answer

up vote 1 down vote accepted

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);
share|improve this answer
    
Thank you so much for the help. I can't believe I forgot that! :) –  AllieCat Jul 22 '13 at 17:19
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.