最近在使用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);

解决方案 »

  1.   

    $urlstr= "http://localhost:8088/solr/update?commit=true" ;
    $data = array('id'=> "5093333",
                    'price'=>" 'set':'100' ",
                  );
    $data = json_encode($data);-d 是否为 post 方式,请先看一下文档
      

  2.   

    谢谢 xuzuning的热心,尝试了一下,暂时还不行,估计还算 "price":{"set":100}这个转义的问题 。
      

  3.   

    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'),
                  );
      

  4.   

    按此操作,返回以下异常 。Error: 4
    4001Unexpected character '{' (code 123) in prolog; expected '<' at [row,col {unknown-source}]: [1,1]400
      

  5.   

    你是在本地调试,我们又访问不到
    慢慢琢磨吧既然 curl 'localhost:8983/solr/update?commit=true' -H 'Content-type:application/json' -d '[{"id":"123","price":{"set":100}}]' 可以
    那你就 $data = '[{"id":"123","price":{"set":100}}]' 试试
      

  6.   

    无奈,还是同样的问题。
    还是感谢 xuzuning的热心
      

  7.   

    再发一遍。
    $ curl http://localhost:8983/solr/update
      -H 'Content-type:application/json' 
      -d '[ {"id"       : "book1",
             "author"   : {"set":"Neal Stephenson"},
             "cat"      : {"add":"Cyberpunk"}
            }
          ]'
      

  8.   

    header 的格式不对
    $header = array("Content-type:application/json", "Accept-Charset:charset=utf-8");
    用冒号:分割
    不知道你的是不是错在这里,我之前就是因为这个出错了。
      

  9.   

    还是没写对
    $header = array("Content-type:application/json", "Accept-Charset:utf-8");
      

  10.   

    首先说下-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");