cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
2655
Views
0
Helpful
3
Replies

REST API POST not working with CURL in PHP

Tim Harber
Level 1
Level 1

Hey All,

 

I am writing some PHP to do various tasks to my ACI environment from a web GUI I am building.  I am able to submit GET requests to my APIC ok with no issue, but I am not receiving any response when I do any post...even a simple login.  Below is my code.  The POST request works fine from Postman.  Anyone have any thoughts why I am not getting any response back from those of you who have done something similar?

 

 

<?php    

	$url = 'https://labapic1/api/aaaLogin.xml';
	$data = '<aaaUser name="myloginID" pwd="myPassword" />';	

	$curl = curl_init();
	
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($curl, CURLOPT_URL, $url);
	curl_setopt($curl, CURLOPT_POST, 1);
	curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
	
	//Ignore cert errors...
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
	
	
	$response = curl_exec($curl);


	
	//Check for errors...
	if(!curl_exec($curl)){
		die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
	}


	curl_close($curl);

	
	
	
	
	//Print output to screen
	echo "<hr>";
	echo "Date: ", date("Y-m-d") , "<br>";
	echo "Time: ", date("h:i:sa") , "<br><br>";
	echo "URL: ", $url, "<br><br>";
	echo "Response: ", $response, "<br><br>";

?>

 

3 Replies 3

mario.motschnik
Level 1
Level 1

Hello!

Can you please show an exaple of a working get request to your lab apic with php curl?

Greetings, Mario

Hello!

Maybe you have to set header options for your post. I have seen a lot examples, where a header was set. And maybe you can try it with uncomment the ssl check. I will start making tests in my lab next week.

Greetings, Mario 

Gaurav Gambhir
Cisco Employee
Cisco Employee

I tested your scripts, seems to be working fine, when I look at the browser data we are receiving expected return from the APIC. for some reason it is not printing it properly though.

 

I am no PHP expert, my guess is may be some XML formatting issue with PHP that is is not able to print the return data.

 

php.png

Save 25% on Day-2 Operations Add-On License