Posted in php functions
3:11 am, October 24, 2021

php post data back to a url with a function

this function uses curl to post back data to a url, this acts like a _POST form request from within a function.

Function usage:


the url to post the data back to

e.g: $url = "";


the variables and data to post back to the url, you can add them in a string like the following example

$myvars = 'myvar1=' . $myvar1 . '&myvar2=' . $myvar2;


echo $response = post_data($url, $myvars);


public function post_data($url, $myvars) {
  		//$url = '';
  		//$myvars = 'myvar1=' . $myvar1 . '&myvar2=' . $myvar2;

  		$ch = curl_init( $url );
  		curl_setopt( $ch, CURLOPT_POST, 1);
  		curl_setopt( $ch, CURLOPT_POSTFIELDS, $myvars);
  		curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
  		curl_setopt( $ch, CURLOPT_HEADER, 0);
  		curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);

  		return $response = curl_exec( $ch );

View Statistics
This Week
This Month
This Year

No Items Found.

Add Comment
Type in a Nick Name here
Search Code
Search Code by entering your search text above.

This is my test area for webdev. I keep a collection of code snippits here, mostly for my reference. Also if i find a good site, i usually add it here.

Join me on Substack if you want me to send you a collection of the things i have done or found or read for the week. Or follow me on twitter if you prefer, i dont post much but i probably should!


Random Quote
The mind must be given relaxation; it will arise better and keener after resting. As rich fields must not be forced-for their productiveness, the have no rest, will quickly exhaust them constantlabor will break the vigor of the mind, but if it is released and relaxed a little while, it will recover its powers