Reply

API calls with PHP site

Authority
Posts: 15
8889     0

Hello,

 

I want to write a website with php to import DNS Data to an Infoblox Grid.

I have already written some python scripts and I have read alot of threads in this forum but the easiest commands in php script do not work in my environment. Maybe someone can give me a starting point.

 

First Step: I have written a webpage with a form in php to get all informations about a new DNS record.

 

Second Step: Push this information via API to an Infoblox Grid. Is there a design recommendation for this step? Should the send command be included on the same page, should this be a function or is it the better way to open a new webpage and do this step there?

 

All examples to use the API via php do not work for me. For example this site 

https://community.infoblox.com/t5/API-Integration/Trying-to-update-the-extattrs-VIA-API-in-php/m-p/9...

or

https://community.infoblox.com/t5/API-Integration/Create-a-HOST-with-PHP-cURL-WAPI/m-p/2830#M274

 

I tried first only "GET" commands but even this do not work. Can someone post a "GET" example. I believe from this it is easy to create some "POST" codes and to create new host records.

 

Thank you.

 

Cheers

 

Re: API calls with PHP site

Adviser
Posts: 181
8889     0

 Hi,

 

Here is a sample PHP function that makes WAPI calls.

function wapiCall($url,$uname,$pwd){
	$auth = base64_encode($uname . ":" . $pwd);
	
	$curl = curl_init();
    curl_setopt_array($curl, array(
	  CURLOPT_URL => $url,
	  CURLOPT_RETURNTRANSFER => true,
	  CURLOPT_ENCODING => "",
	  CURLOPT_MAXREDIRS => 10,
	  CURLOPT_TIMEOUT => 30,
	  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
	  CURLOPT_CUSTOMREQUEST => "GET",
	  CURLOPT_HEADER => true,
	  CURLOPT_SSL_VERIFYPEER => false,
	  CURLOPT_HTTPHEADER => array(
        "authorization: Basic " . $auth
	  ),
	));
    $response = curl_exec($curl);
    $err = curl_error($curl);
	// extract header
    $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
    // extract body
	$body = substr($response, $headerSize);
	$fields = json_decode($body);
	curl_close($curl);
	return $fields;
}

Here is sample program that uses the above function.

$gm_ip = "grid-master.xyz.com";
$uname = "admin";
$pwd = "infoblox";

$base_url = "https://" . $gm_ip . "/wapi/v2.9";

// Get the zones 
$url = $base_url . "/zone_auth";
$members = wapiCall($url,$uname,$pwd);

Hope you find this helpful,

Thanks and Regards,

Krishna Vasudevan

Re: API calls with PHP site

Authority
Posts: 15
8889     0

Thank you Krishna,

 

This looks similar to my program. I tried to analyze this by printing return values on the webpage. This does not work and I thought there are mistakes in the code.

How do you test your code? How do you get the return values to use them for further tasks?

Can you print out the vairable $members ?

 

Best regards

Sebastian

Re: API calls with PHP site

Adviser
Posts: 181
8889     0

Hi Sebastian,

 

Since the $members variable is an array, you cannot print ir directly, You cna however use the var_dump option to look at the contents.

 

Here is how you can do that, and the sample output.

 var_dump ($members);
array(7) {
  [0]=>
  object(stdClass)#2 (3) {
    ["_ref"]=>
    string(63) "zone_auth/ZG5zLnpvbmUkLl9kZWZhdWx0LmNvbS5kZW1v:demo.com/default"
    ["fqdn"]=>
    string(8) "demo.com"
    ["view"]=>
    string(7) "default"
  }
  [1]=>
  object(stdClass)#3 (3) {
    ["_ref"]=>
    string(75) "zone_auth/ZG5zLnpvbmUkLl9kZWZhdWx0LmNvbS5sYWItc2V0dXA:lab-setup.com/default"
    ["fqdn"]=>
    string(13) "lab-setup.com"
    ["view"]=>
    string(7) "default"
  }
  [2]=>
  object(stdClass)#4 (3) {
    ["_ref"]=>
    string(59) "zone_auth/ZG5zLnpvbmUkLjEuY29tLmRlbW8:demo.com/ansible-test"
    ["fqdn"]=>
    string(8) "demo.com"
    ["view"]=>
    string(12) "ansible-test"
  }
  [3]=>
  object(stdClass)#5 (3) {
    ["_ref"]=>
    string(88) "zone_auth/ZG5zLnpvbmUkLjEuYXJwYS5pbi1hZGRyLjE5Mi4xNjguMTE:192.168.11.0%2F24/ansible-test"
    ["fqdn"]=>
    string(15) "192.168.11.0/24"
    ["view"]=>
    string(12) "ansible-test"
  }
  [4]=>
  object(stdClass)#6 (3) {
    ["_ref"]=>
    string(92) "zone_auth/ZG5zLnpvbmUkLl9kZWZhdWx0LmFycGEuaW4tYWRkci4xOTIuMTY4LjEx:192.168.11.0%2F24/default"
    ["fqdn"]=>
    string(15) "192.168.11.0/24"
    ["view"]=>
    string(7) "default"
  }
  [5]=>
  object(stdClass)#7 (3) {
    ["_ref"]=>
    string(61) "zone_auth/ZG5zLnpvbmUkLl9kZWZhdWx0Lm5ldC5hYmM:abc.net/default"
    ["fqdn"]=>
    string(7) "abc.net"
    ["view"]=>
    string(7) "default"
  }
  [6]=>
  object(stdClass)#8 (3) {
    ["_ref"]=>
    string(61) "zone_auth/ZG5zLnpvbmUkLl9kZWZhdWx0LmNvbS5hYmM:abc.com/default"
    ["fqdn"]=>
    string(7) "abc.com"
    ["view"]=>
    string(7) "default"
  }
}

If you can paste your code here, I can look into it.

Thanks and Regards,

Krishna Vasudevan

Re: API calls with PHP site

Authority
Posts: 15
8889     0

I have expected that I have to take care about the data structure. I thought the return vlaue is Json. Thank you for the hint with the array.

Also your example do not work in my environment. I have combined your lines

 

<?php

$gm_ip = "172.30.20.104";
$uname = "admin";
$pwd = "infoblox";

$base_url = "https://" . $gm_ip . "/wapi/v2.9";

 

// Get the zones
$url = $base_url . "/zone_auth";
$members = wapiCall($url,$uname,$pwd);
var_dump ($members);

 

?>

<?php

function wapiCall($url,$uname,$pwd){
$auth = base64_encode($uname . ":" . $pwd);

$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HEADER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HTTPHEADER => array(
"authorization: Basic " . $auth
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
// extract header
$headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
// extract body
$body = substr($response, $headerSize);
$fields = json_decode($body);
curl_close($curl);
return $fields;
}

?>

 

I do not get any output.

 

I can also paste my code of my whole webpage but at first I want to understand the basic interaction and how I can handle the return value.

 

Thank you again for your help.

 

Best regards

Sebastian 

Re: API calls with PHP site

Adviser
Posts: 181
8889     0

Hi  Sebastian,

 

To debug and check if the curl call is going through, add a couple of var_dump messages within the function, like below:

function wapiCall($url,$uname,$pwd){
	$auth = base64_encode($uname . ":" . $pwd);
	
	$curl = curl_init();
    curl_setopt_array($curl, array(
	  CURLOPT_URL => $url,
	  CURLOPT_RETURNTRANSFER => true,
	  CURLOPT_ENCODING => "",
	  CURLOPT_MAXREDIRS => 10,
	  CURLOPT_TIMEOUT => 30,
	  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
	  CURLOPT_CUSTOMREQUEST => "GET",
	  CURLOPT_HEADER => true,
	  CURLOPT_SSL_VERIFYPEER => false,
	  CURLOPT_HTTPHEADER => array(
        "authorization: Basic " . $auth
	  ),
	));
    var_dump($curl);
    $response = curl_exec($curl);
    var_dump($response);
    $err = curl_error($curl);
    var_dump($error);
	// extract header
    $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
    // extract body
	$body = substr($response, $headerSize);
	$fields = json_decode($body);
	curl_close($curl);
	return $fields;
}

We will be able to identify where you are hitting an issue.

Regards,

Krishna Vasudevan

Re: API calls with PHP site

Authority
Posts: 15
8889     0

I copied your lines but I do not get any var_dump output.

I get only an HTTP Error 500. When include any simple ouptut like

echo "test output";

I got only an empty page with this one line. So I get no output from the var_dump command.

 

Best regards

Sebastian 

Re: API calls with PHP site

Adviser
Posts: 181
8889     0

Hi,

 

Can you run a simple API call as below, and check if you get any output?

curl -k -u admin:infoblox -X GET "https://grid-master/wapi/v2.9/zone_auth"

This will help understanding if the API call itself is going through.

 

Regards,

Krishna

Re: API calls with PHP site

Authority
Posts: 15
8889     0

Yes this works. API calls from the command line and the print out in a browser work.

Re: API calls with PHP site

[ Edited ]
Authority
Posts: 15
8889     0

Has someone another idea that I can test to identify the failure?

Thank you.

Showing results for 
Search instead for 
Did you mean: 

Recommended for You