PHP POST curl terminal command in PHP script

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

There are two problems:

  • You need to properly quote $myjson . Read the difference between single and double quoted strings in PHP.
  • 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);
    
    链接地址: http://www.djcxy.com/p/8556.html

    上一篇: 使用cURL将Json数据发送到另一台服务器

    下一篇: PHP脚本中的PHP POST curl终端命令