最近在使用Apache Solr.
以下 curl 'localhost:8983/solr/update?commit=true' -H 'Content-type:application/json' -d '[{"id":"123","price":{"set":100}}]' 在linux系统中调用成功,但是在PHP中不清楚如何正确调用.
请分析以下代码: $ch = curl_init();
$data = array('id'=> "5093333",
'price'=>" 'set':'100' ",
'commit' => 'true');
$urlstr= "http://localhost:8088/solr/update" ;
$header = array("Content-type;application/json;charset=utf-8"); curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_URL,$urlstr);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
以下 curl 'localhost:8983/solr/update?commit=true' -H 'Content-type:application/json' -d '[{"id":"123","price":{"set":100}}]' 在linux系统中调用成功,但是在PHP中不清楚如何正确调用.
请分析以下代码: $ch = curl_init();
$data = array('id'=> "5093333",
'price'=>" 'set':'100' ",
'commit' => 'true');
$urlstr= "http://localhost:8088/solr/update" ;
$header = array("Content-type;application/json;charset=utf-8"); curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_URL,$urlstr);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
$data = array('id'=> "5093333",
'price'=>" 'set':'100' ",
);
$data = json_encode($data);-d 是否为 post 方式,请先看一下文档
-d/--data <data> HTTP POST方式传送数据 print_r(json_decode('[{"id":"123","price":{"set":100}}]', 1));
得 Array
(
[0] => Array
(
[id] => 123
[price] => Array
(
[set] => 100
)
)
)
所以 $data 应为
$data = array('id'=> "5093333",
'price'=> array('set' => '100'),
);
4001Unexpected character '{' (code 123) in prolog; expected '<' at [row,col {unknown-source}]: [1,1]400
慢慢琢磨吧既然 curl 'localhost:8983/solr/update?commit=true' -H 'Content-type:application/json' -d '[{"id":"123","price":{"set":100}}]' 可以
那你就 $data = '[{"id":"123","price":{"set":100}}]' 试试
还是感谢 xuzuning的热心
$ curl http://localhost:8983/solr/update
-H 'Content-type:application/json'
-d '[ {"id" : "book1",
"author" : {"set":"Neal Stephenson"},
"cat" : {"add":"Cyberpunk"}
}
]'
$header = array("Content-type:application/json", "Accept-Charset:charset=utf-8");
用冒号:分割
不知道你的是不是错在这里,我之前就是因为这个出错了。
$header = array("Content-type:application/json", "Accept-Charset:utf-8");
代表的意思是,你发送的是 “ '[{"id":"123","price":{"set":100}}]' ”,带有单引号,而如果你要用php发送的话,数据也必须保证是“ '[{"id":"123","price":{"set":100}}]' ”你应该使用
$data = '\'[{"id":"123","price":{"set":100}}]\'';
做测试,基本是用json格式也应该是
$data = array('id'=> "5093333",
'price'=> array('set' => '100'),
);$data = "'" . json_encode($data) . "'";头部使用8楼的建议:
$header = array("Content-type:application/json;charset=UTF-8");