0

The following URL is from the FobBugz API documentation: https://kakapo.fogbugz.com/api.asp?cmd=search&q=project:inbox%20assignedTo:Elinor&cols=ixProject,ixPersonAssignedTo,sTitle&max=2&token=04t9123822q4kbba09nt740inhibk2 (you can find it here)

If I copy and paste the above URL into a web browser I get an XML response. What I would like to do is create a function that returns the XML response as a its result.

I am so stuck, it is simply not working. All I seem to get in response is an empty string. When I use this 'example' on the FogBugz site I get XML telling me that I am NOT logged in.

The function below comes mostly from here: Make a HTTPS request through PHP and get response I have been messing with it for hours without success.

function searchBug(){

$data =  "https://kakapo.fogbugz.com/api.asp?cmd=search&q=project:inbox%20assignedTo:Elinor&cols=ixProject,ixPersonAssignedTo,sTitle&max=2&token=04t9123822q4kbba09nt740inhibk2"; 
    //echo $data;
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_URL, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, False);
    $result = curl_exec($ch);
    curl_close($ch);
    echo $result;
}

[EDIT: in response to comment] The response that I want to receive is:

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<response>
<error code="3">
<![CDATA[ Not logged in ]]>
</error>
</response>

As that is what is shown in my browser when I paste the URL and press enter.

Community
  • 1
  • 1
SlowLearner
  • 3,086
  • 24
  • 54
  • And what do you want to receive? – mcklayin May 08 '17 at 11:06
  • Well, the error is allready show, you are simply just not logged in, you probaly need to make a `login` `call` first. – Red May 08 '17 at 11:15
  • i tried it, it works for me – mcklayin May 08 '17 at 11:15
  • @mrweinh - in response to your deleted answer, I have an account and API token but since I get a result from the example I figured that a PHP script should return the same. – SlowLearner May 08 '17 at 11:15
  • I deleted my answer because I misunderstood your question. You weren't wondering why the response was that you are not logged in, you were wondering why the PHP script did not receive any response, right? – taalas May 08 '17 at 11:17
  • @mrwienh Yes... using my own token and being logged in I still get nothing. But when I paste the exact same URL into the browser I get the expected result. Oddly I can do this with in visual basic using MSXML2.XMLHTTP object.... but that is not helping me either :-/ – SlowLearner May 08 '17 at 11:19
  • Have you tried to check what curl_error() returns? – taalas May 08 '17 at 11:20
  • @mcklayin - are you saying that you have an XML response using PHP? – SlowLearner May 08 '17 at 11:20
  • @mrwienh - I'll google how to do that, but if you can provide the line(s) of code to achieve that I'd appreciate... very new to PHP – SlowLearner May 08 '17 at 11:22
  • You can catch errors during the call like so: http://stackoverflow.com/questions/3987006/how-to-catch-curl-errors-in-php – taalas May 08 '17 at 11:23
  • 1
    @SlowLearner yes, if you want to see you xml response in browser, don't forget to send xml headers – mcklayin May 08 '17 at 11:27
  • Added the error handler from above (and changed to $ch) but still get the same – SlowLearner May 08 '17 at 11:41
  • @mcklayin I think the problem was XML headers... its working now. Thanks. – SlowLearner May 08 '17 at 11:42

1 Answers1

2

You can do it like below:-

<?php
function searchBug($path){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$path);
    curl_setopt($ch, CURLOPT_FAILONERROR,1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 15);
    $retValue = curl_exec($ch);          
    curl_close($ch);
    return $retValue;
}

$sXML = searchBug('https://kakapo.fogbugz.com/api.asp?cmd=search&q=project:inbox%20assignedTo:Elinor&cols=ixProject,ixPersonAssignedTo,sTitle&max=2&token=04t9123822q4kbba09nt740inhibk2');
header('Content-Type: text/xml');
echo $sXML;

Output:- http://prntscr.com/f5fj44

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98