Friday, 15 June 2012

algorithm - Send AJAX-like post request using PHP only -



algorithm - Send AJAX-like post request using PHP only -

i'm working on automatization script in php (no html!). have 2 php files. 1 executing script, , 1 receive $_post info , returns information. question how 1 php script send post php script, homecoming variables , go on working on first script without html form , no redirects. need create requests couple of times first php file under different conditions , homecoming different type of data, depending on request. have this:

<?php // action.php (first php script) /* doing stuff */ $data = sendpost('get_info');// send post getinfo.php attribute ['get_info'] , homecoming info file $mysqli->query("insert domains (id, name, address, email) values('".$data['id']."', '".$data['name']."', '".$data['address']."', '".$data['email']."')") or die(mysqli_error($mysqli)); /* go on doing stuff */ $data2 = sendpost('what_is_the_time');// send post getinfo.php attribute ['what_is_the_time'] , homecoming time info file sendpost('get_info' or 'what_is_the_time'){ //do post desired attribute homecoming $data; } ?>

i think need function called attribute, sending post request , returning info based on request. , sec php file:

<?php // getinfo.php (another php script) if($_post['get_info']){ //do actions $data = anotherfunction(); homecoming $data; } if($_post['what_is_the_time']){ $time = time(); homecoming $time; } function anotherfunction(){ //do stuff homecoming $result; } ?>

thanks in advance guys.

update: ok. curl method fetching output of php file. how homecoming $data variable instead of whole output?

you should utilize curl. function this:

function sendpost($data) { $ch = curl_init(); // should set here url of getinfo.php script curl_setopt($ch, curlopt_url, "getinfo.php"); curl_setopt($ch, curlopt_returntransfer, true); curl_setopt($ch, curlopt_post, 1); curl_setopt($ch, curlopt_postfields, $data); $result = curl_exec ($ch); curl_close ($ch); homecoming $result; }

then should phone call way:

$data = sendpost( array('get_info'=>1) );

php algorithm function http-post

No comments:

Post a Comment