Question

here is sample code that i used to fetch project from api:

$ch = curl_init('https://www.bugherd.com/api_v2/projects.json');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array('api key'));                                                                                                                   
$result = curl_exec($ch);
$json= json_decode($result, true);
print_r($json);

output is here

Array
(
[error] => invalid api key
)

api key is correct so where im wrong in there api they have mention to fetch projects

curl -u api key:x https://www.bugherd.com/api_v2/organization.json
Was it helpful?

Solution

Use a basic HTTP Authentication as the documentation of the API says..

<?php
$curl = curl_init('https://www.bugherd.com/api_v2/projects.json');
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, 'api key:x');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$return = curl_exec($curl);
$json= trim($return,'1');
$jsdecarr = json_decode($json,true);
print_r($jsdecarr);

OUTPUT :

Array
(
    [projects] => Array
        (
            [0] => Array
                (
                    [id] => 39921
                    [name] => bugherd addon
                )

            [1] => Array
                (
                    [id] => 40083
                    [name] => abcd
                )

            [2] => Array
                (
                    [id] => 40250
                    [name] => fsdfgdg
                )

        )

    [meta] => Array
        (
            [count] => 3
        )

)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top