我需要将这个“普通”卷曲字符串转换为php curl。此命令经过测试,可以在shell中使用:
curl -XPUT http://foo/monitors/1.json -d "Monitor[Name]=test1"
我已经看了很多关于它的例子,但到目前为止我的尝试都没有结果。这是我尝试过的最新消息:
$data = array("Name" => "test1");
$payload = json_encode( array( "Monitor"=> $data ) );
$url = 'hxxp://foo/monitors/1.json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS,$payload);
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch)
我试图放入的json格式如下:
{"monitor":{"Monitor":{"Id":"1","Name":"test","Type":"Remote","Funct.....
感谢
*更新了更多当前信息
答案 0 :(得分:0)
$data_string =""/* your json string goes here like {"monitor":{"Monitor":{"Id":"1","Name":"test","Type":"Remote"}} */
$ch = curl_init('http://api.local/rest/users');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
答案 1 :(得分:0)
相当于您的命令行示例:
$payload = "Monitor[Name]=test1";
$url = 'http://localhost/monitors/1.json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS,$payload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
因为你的样本没有使用" application / json"作为"内容类型"但实际上股票" application / x-www-form-urlencoded"价值和"数据"不是真正的JSON