在linux下使用下面命令可以执行:
curl -v -S -u devuser:devuser123 -F'notification={"applicationId":"5","schemaId":"12","topicId":"1","type":"USER"};type=application/json' -F [email protected] "http://135.252.37.213:8080/kaaAdmin/rest/api/sendNotification"
我在php中代码如下:
  <?php
    $ch = curl_init();
      
    $user="devuser:devuser123";
    curl_setopt($ch, CURLOPT_USERPWD,$user);   //user:passwd
    
    
    
    curl_setopt($ch,CURLOPT_URL,"http://135.252.37.213:8080/kaaAdmin/rest/api/sendNotification");  //url set
    
    
    curl_setopt($ch, CURLOPT_VERBOSE, 1);    //-v 
    
    
    curl_setopt($ch, CURLOPT_POST, 1);    //-F 以表单方式
    
    //post data
    $post_data = array(
        'notification' => array(
           'applicationId' => '5',
            'schemaId' => '12',
            'topicId' => '1',
            'type' => 'USER'
        ),
        'type'=>'application/json',
        'file'=>'@D:\WAMP\www\kaa\notification.json'       
     );
    
       
     curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
    
    
    $response = curl_exec($ch);
    
    if($response  === FALSE)
    {
     echo "cURL 具体出错信息: " . curl_error($ch);     
    }
    
    
    curl_close($ch);
  ?> 返回结果如下:HTTP ERROR 415Problem accessing /kaaAdmin/rest/api/sendNotification. Reason:    Unsupported Media Type请高手帮忙看看,多谢!

解决方案 »

  1.   

    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
    应写作
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    因为 http_build_query 破坏了 @D:\WAMP\www\kaa\notification.json 的表述但是 对于多维数组的 $post_data 还是存在问题
    因为 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 只接受一维数组你可以改变 $post_data 的结构 或 分开来提交,但这都涉及到对方的程序
      

  2.   

    ls同学。我改成下面这样还是不行    
                $data_str=json_encode($post_data);   
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_str);另外能帮忙解释一下那条curl命令吗,尤其是红色部分,多谢
    curl -v -S -u devuser:devuser123 -F'notification={"applicationId":"5","schemaId":"12","topicId":"1","type":"USER"};type=application/json' -F [email protected] "http://135.252.37.213:8080/kaaAdmin/rest/api/sendNotification"
      

  3.   

    $post_data = array(
      'notification ' => '{"applicationId":"5","schemaId":"12","topicId":"1","type":"USER"}',
      'type' => 'application/json',
      'file' => '@D:\WAMP\www\kaa\notification.json'
    );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    这样应该可以
      

  4.   


    版主,我按照你的提示修改代码如下,但是还是有错误
    <html>
    <head>
    <title> post data</title>
      </head>


    <body>   <?php
      
      
        $ch = curl_init();
          
        $user="devuser:devuser123";
        curl_setopt($ch, CURLOPT_USERPWD,$user);   //user:passwd
        
        
        
        curl_setopt($ch,CURLOPT_URL,"http://135.252.37.213:8080/kaaAdmin/rest/api/sendNotification");  //url set
        
        
        curl_setopt($ch, CURLOPT_VERBOSE, 1);    //-v 
        
        
        curl_setopt($ch, CURLOPT_POST, 1);    //-F 
        
        //post data
        
        $post_data = array(
            'notification' => '{"applicationId":"5","schemaId":"12","topicId":"1","type":"USER"}',
            'type'=>'application/json',
            'file'=>'@D:\WAMP\www\kaa\notification.json'       
         );
          
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
                 
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                 
        $response = curl_exec($ch);
        
        echo $response;
        
        if($response  === FALSE)
        {
         echo "cURL 错误信息: " . curl_error($ch);     
        }
          
        curl_close($ch);
          
      ?>


      </body>

    </html> 返回结果:
    HTTP ERROR 400Problem accessing /kaaAdmin/rest/api/sendNotification. Reason:    Required request part 'notification' is not present
    Powered by Jetty://
    再帮忙看看吧,多谢
      

  5.   

    400 请求出错 
    由于语法格式有误,服务器无法理解此请求。这应该不是你的问题
    因为你的代码在我这里测试成功(当然目标url和上传文件名是要换的)
      

  6.   

    按需要看server端的restful 接口吗?
      

  7.   

    curl -v -S -u devuser:devuser123 -F'notification={"applicationId":"5","schemaId":"12","topicId":"1","type":"USER"};type=application/json' -F [email protected] "http://135.252.37.213:8080/kaaAdmin/rest/api/sendNotification"我仔细理解了一下这条命令,-F是以表单格式提交,对于第一个-F,type表示 提交的是json数据,对于第二个-F,是按照默认格式提交的,那么在PHP中,不同格式的数据能同时提交吗?还是说要看server侧的具体实现?thanks