Bugzilla REST接口 - 如何为POST请求提供API密钥和身份验证

时间:2016-09-14 11:37:11

标签: php json rest curl

我能够将GET Request(请点击链接以查看我编写的程序以发出GET请求)到Bugzilla REST API,因为它不需要用户名/密码进行身份验证。但是,创建错误(POST请求)需要用户名和密码。我还生成了API密钥,但无法找到有关如何在PHP程序中使用API​​密钥来对Bugzilla Server进行REST POST调用的文档。当我执行以下程序时,出现错误:您必须在使用Bugzilla的这部分之前登录。代码:410。感谢您解决问题的任何帮助

       $url ="http://localhost:8080/bugzilla/rest/bug";
       $apikey = "IZC4rs2gstCal0jEZosFjDBRV9AQv2gF0udh4hgq";
       $data = array(
            "product" => "TestProduct",
            "component" => "TestComponent",
            "version" => "unspecified",
            "summary" => "This is a test bug - please disregard",
            "alias" => "SomeAlias",
            "op_sys" => "All",
            "priority" => "P1",
            "rep_platform" => "All"
        );

        $str_data = json_encode($data);

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");  
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS,$str_data);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
        curl_setopt($ch, CURLOPT_HTTPHEADER, 
                array("Content-Type: application/json", "Accept: application/json")); 
        $username = "ashish.sureka@in.abb.com";
        $password = "abbincrc";
        curl_setopt($ch, CURLOPT_USERPWD, $username.":".$password);
        $result = curl_exec($ch);
        curl_close($ch); 

        echo $result

1 个答案:

答案 0 :(得分:-1)

以下代码对我有用并解决了我的问题。我在这里发布它,以便对其他人有用。我也有written a blog on it

        <?php

        $url = 'http://localhost:8080//bugzilla/xmlrpc.cgi';
        $ch = curl_init();

        $header = array(
            CURLOPT_URL     => $url,
            CURLOPT_POST    => true,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HTTPHEADER  => array( 'Content-Type: text/xml', 'charset=utf-8' )
        );
        curl_setopt_array($ch, $header);

        $bugreport = array(
            'login'       => 'ashish.sureka@in.abb.com',
            'password'    => 'abbincrc',
            'product'     => "TestProduct",
            'component'   => "TestComponent",
            'summary'     => "Bug Title :  A One Line Summary",
            'assigned_to' => "ashish.sureka@in.abb.com",
            'version'     => "unspecified",
            'description' => "Bug Description : A Detailed Problem Description",
            'op_sys'      => "All",
            'platform'    => "All",
            'priority'    => "Normal",
            'severity'    => "Trivial"
        );

        $request = xmlrpc_encode_request("Bug.create", $bugreport); 
        curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
        curl_exec($ch)

      ?>