请高手分析一下 ,PHP 利用cURL函数中调用以下Apache Solr语句为何出错

steed921 2012-11-19 02:30:15
最近在使用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);
...全文
335 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
lazyboy_wu 2013-05-29
  • 打赏
  • 举报
回复
首先说下
引用
curl 'localhost:8983/solr/update?commit=true' -H 'Content-type:application/json' -d '[{"id":"123","price":{"set":100}}]'
-d '[{"id":"123","price":{"set":100}}]' 代表的意思是,你发送的是 “ '[{"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");
AlwaysToBeZero 2013-05-29
  • 打赏
  • 举报
回复
还是没写对。。。 $header = array("Content-type:application/json", "Accept-Charset:utf-8");
AlwaysToBeZero 2013-05-29
  • 打赏
  • 举报
回复
header 的格式不对 $header = array("Content-type:application/json", "Accept-Charset:charset=utf-8"); 用冒号:分割 不知道你的是不是错在这里,我之前就是因为这个出错了。
steed921 2012-11-19
  • 打赏
  • 举报
回复
再发一遍。 $ curl http://localhost:8983/solr/update -H 'Content-type:application/json' -d '[ {"id" : "book1", "author" : {"set":"Neal Stephenson"}, "cat" : {"add":"Cyberpunk"} } ]'
steed921 2012-11-19
  • 打赏
  • 举报
回复
无奈,还是同样的问题。 还是感谢 xuzuning的热心
xuzuning 2012-11-19
  • 打赏
  • 举报
回复
你是在本地调试,我们又访问不到 慢慢琢磨吧 既然 curl 'localhost:8983/solr/update?commit=true' -H 'Content-type:application/json' -d '[{"id":"123","price":{"set":100}}]' 可以 那你就 $data = '[{"id":"123","price":{"set":100}}]' 试试
steed921 2012-11-19
  • 打赏
  • 举报
回复
按此操作,返回以下异常 。 Error: 4 4001Unexpected character '{' (code 123) in prolog; expected '<' at [row,col {unknown-source}]: [1,1]400
xuzuning 2012-11-19
  • 打赏
  • 举报
回复
linux curl 命令详解,以及实例 -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'), );
steed921 2012-11-19
  • 打赏
  • 举报
回复
谢谢 xuzuning的热心,尝试了一下,暂时还不行,估计还算 "price":{"set":100}这个转义的问题 。
引用 1 楼 xuzuning 的回复:
$urlstr= "http://localhost:8088/solr/update?commit=true" ; $data = array('id'=> "5093333", 'price'=>" 'set':'100' ", ); $data = json_encode($data); -d……
xuzuning 2012-11-19
  • 打赏
  • 举报
回复
$urlstr= "http://localhost:8088/solr/update?commit=true" ; $data = array('id'=> "5093333", 'price'=>" 'set':'100' ", ); $data = json_encode($data); -d 是否为 post 方式,请先看一下文档

20,359

社区成员

发帖
与我相关
我的任务
社区描述
“超文本预处理器”,是在服务器端执行的脚本语言,尤其适用于Web开发并可嵌入HTML中。PHP语法利用了C、Java和Perl,该语言的主要目标是允许web开发人员快速编写动态网页。
phpphpstorm 技术论坛(原bbs)
社区管理员
  • 开源资源社区
  • phpstory
  • xuzuning
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧