This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

API call using curl & PHP

Greetings, 

 I've been working with the API a bit, and i'm wondering if someone could help me out.

Normally, I would call an API something like this very basic example:

<?php
$ch = curl_init('example.com/.../test);

$data = curl_exec($ch);

$info = curl_getinfo($ch);

curl_close($ch)

?>

I'm attempting to call the API and get a list of the users. I can accomplish this in a browser, which returns an xml of user data.

192.168.11.234:4444/.../APIController

 

I'm wondering if anyone has any suggestions as to how to call the API using a PHP script that can return the data.  sorry, but the sophos API documentation is not so great. So far no luck. Thought it might be as simple as this:

<?php
$ch = curl_init('192.168.11.234:4444/.../APIController);

$data = curl_exec($ch);

$info = curl_getinfo($ch);

curl_close($ch)

?>

Just trying to figure out ways of how to work with the API, I am hoping to perhaps get an output of an array that I can work with and send back modifications as an Xml.

Thanks for any help anyone may be able to offer. 



This thread was automatically locked due to age.
  • Looks like my api link got altered upon submission.  I'm currently calling the api in a browser like this:

    <IP of XG>:4444/webconsole/APIController?reqxml=<Request><Login><Username>xxxxx</Username><Password>xxxxx</Password></Login><Get><User></User></Get></Request>

  • This is my approach, but not luck so far:

    <?php

    $input_xml = '<Request><Login><Username>xxxx</Username><Password>xxxxx</Password></Login><Get><User></User></Get></Request>';
    $url = "192.168.11.234:4444/.../APIController";


    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_POSTFIELDS,
    "reqxml=" . $input_xml);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
    $data = curl_exec($ch);
    curl_close($ch);

    //convert the XML result into array
    $array_data = json_decode(json_encode(simplexml_load_string($data)), true);

    print_r('<pre>');
    print_r($array_data);
    print_r('</pre>');
    ?>

  • Maybe this will help someone, but this works for me:

    $input_xml = '<Request><Login><Username>XXXXXX</Username><Password>XXXXX</Password></Login><Get><User></User></Get></Request>';
    $url = "192.168.11.234:4444/.../APIController";
    $ch = curl_init();
    $headers = [
    "Content-type: text/xml;charset=\"utf-8\"",
    "Accept: text/xml",
    "Content-length: ".strlen($xml_data)
    ];

    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);



    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_URL, $url."?reqxml=" . urlencode($input_xml));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    if (curl_error($ch)) {
    echo 'error:' . curl_error($ch);
    }
    $data = curl_exec($ch);
    var_dump($data);
    curl_close($ch);


    $array_data = json_decode(json_encode(simplexml_load_string($data)), true);

    print_r('<pre>');
    print_r($array_data);
    print_r('</pre>');

     

    This outputs an array of the users.