Link to home
Start Free TrialLog in
Avatar of ITNC
ITNCFlag for United States of America

asked on

PHP Use CURL to post an array to URL

Can someone give me an example on how to take a multi-dimensional array ($params) and post that array to a URL. I then need to be able to use 'extract' on the array to create variables on the remote end.
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Please post the $params array so we can see exactly what the data looks like.  Sending a multidimensional array is a very, very rare occurrence.  More likely you will want to take a mainstream approach, such as sending a JSON string.
Avatar of J N
J N

Here is a working curl function which you can use to cURL to GET POST or DELETE

function CURL($url, $json, $action){
		try{
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
			curl_setopt($ch, CURLOPT_MAXREDIRS, 10 );
			curl_setopt($ch, CURLOPT_URL, $url);
		   
		   //
			switch($action){
					case "POST":
					curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
					curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
					break;
			 
				case "GET":
					curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
					break;
			 
				case "DELETE":
					curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
					curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
			 
				default:
					break;
			}
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_TIMEOUT, 10);
			$output = curl_exec($ch);
			
			//END CURL
			curl_close($ch);
			
			//RETURN DATA
			return $output;
		
		}catch(Excetion $e){
				//THE CURL WAS UNSUCCESSFUL	
				echo '<div>Unable to get data!</div>';
				
				//RETURN FALSE
				return false;
		}
	}

Open in new window

Take a look this example link for curl  post
http://davidwalsh.name/curl-post

And php.net also provide the manual for curl
http://php.net/manual/en/function.curl-setopt.php

Duncan
Avatar of ITNC

ASKER

Here is an example of the $params array:

Array
(
    [accountid] => 5
    [serviceid] => 5
    [userid] => 1
    [domain] => 
    [username] => test
    [password] => test
    [packageid] => 9
    [pid] => 9
    [serverid] => 4
    [type] => other
    [producttype] => other
    [moduletype] => test
    [configoption1] => TEST123
    [configoption2] => 
    [configoption3] => 
    [configoption4] => 
    [configoption5] => 
    [configoption6] => 
    [configoption7] => 
    [configoption8] => 
    [configoption9] => 
    [configoption10] => 
    [configoption11] => 
    [configoption12] => 
    [configoption13] => 
    [configoption14] => 
    [configoption15] => 
    [configoption16] => 
    [configoption17] => 
    [configoption18] => 
    [configoption19] => 
    [configoption20] => 
    [configoption21] => 
    [configoption22] => 
    [configoption23] => 
    [configoption24] => 
    [customfields] => Array
        (
            [*Note] => 
        )

    [configoptions] => Array
        (
            [MSRP] => MSRP
            [Wan Failover] => OFF
            [Passthru] => OFF
            [DHCP] => ON
            [DNS] => ON
            [NAT] => ON
        )

    [clientsdetails] => Array
        (
            [userid] => 1
            [id] => 1
            [firstname] => test
            [lastname] => test
            [fullname] => test test
            [companyname] => test
            [email] => test@test.com
            [address1] => 23605 test Rd
            [address2] => 23605 test Rd
            [city] => test
            [fullstate] => test
            [state] => te
            [postcode] => 11111
            [countrycode] => US
            [country] => US
            [statecode] => te
            [countryname] => United States
            [phonecc] => 1
            [phonenumber] => 111-111-1111
            [phonenumberformatted] => +1.1111111111
            [billingcid] => 0
            [notes] => 
            [password] => test
            [twofaenabled] => 
            [currency] => 1
            [defaultgateway] => 
            [cctype] => 
            [cclastfour] => 
            [securityqid] => 0
            [securityqans] => 
            [groupid] => 0
            [status] => Active
            [credit] => 924.00
            [taxexempt] => 
            [latefeeoveride] => 
            [overideduenotices] => 
            [separateinvoices] => 
            [disableautocc] => 
            [emailoptout] => 0
            [overrideautoclose] => 0
            [language] => 
       
        )

    [server] => 1
    [serverip] => 
    [serverhostname] => test.test.net
    [serverusername] => 
    [serverpassword] => 
    [serveraccesshash] => testhash
    [serversecure] => 
    [action] => create
)

Open in new window

Hi,

can you not json_econde the array and then use it in my function above?

ex:
 $json=json_encode($params);
$url='www.somewhere.com';
$data=CURL($url, $json, 'POST')

echo '<pre>';
print_r($data);
echo'</pre>';

Open in new window

Avatar of ITNC

ASKER

I will try that and get back with you
Avatar of ITNC

ASKER

M. Jayme Nagy,

Should I be able to grab the posted data on the remote end using $_POST['data'] ?
Your choice of GET or POST will depend on whether the request can change the state of the server.  If the request is idempotent, you can use GET.  If the request is not idempotent you must use POST.  If you're not sure what idempotent means, use POST (but look it up, because it's an important concept in computer science).

It's not clear to me what the data elements in the $_GET or $_POST array will look like.  On the target server, you will want to use a combination of output buffering and var_dump() to capture the request data.  Then you can use error_log or similar to put the request data into a place where it will be visible.  You might email it to yourself.  The reason you have to jump through some extra hoops is because the cURL request is made "server-to-server" and there is no browser output along the way.

The lack of browser output means that it's somewhat difficult to debug communication errors.  Only the sending server (which is acting as a client) will be able to talk to you about any problems.  You might want to be familiar with curl errors and you might want to use a script that will visualize these in case any of them occur.
http://curl.haxx.se/libcurl/c/libcurl-errors.html
echo '<pre>';
print_r($data);
echo'</pre>';

will return the result (changes for POST DELETE or GET) from the receiving server. the receiving server should know how to interpret the data. Its difficult to guide you without adequate information and @ray is one of the forums best
Avatar of ITNC

ASKER

So right now on the remote end I have the following:

$vars = print_r($_POST);
echo "POST: " . $vars . "";

And the result I get is:
POST: 1

Any reason why it is returning a 1?
try

echo '<pre>';
 print_r($_REQUEST);
echo '</pre>';
Avatar of ITNC

ASKER

That also prints a 1
All of the PHP functions are documented on the PHP.net web site. If you're not sure why a function is doing something (like returning "1") you can look it up by going to http://php.net and typing the function name into the search box at the upper right.  Example here: http://php.net/manual/en/function.print-r.php
what are you trying to get?
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ITNC

ASKER

Ray,

I was able to get the array I needed using your script above. Thank you all for your expertise and quick responses. I have much to learn but this helps tremendously.
Great!  Thanks for using E-E, it can be a great learning community.  All the best, ~Ray