Posted in php functions
3135
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:

$url

the url to post the data back to

e.g: $url = "https://kruxor.com/";

$myvars

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;

Usage

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

PHP

public function post_data($url, $myvars) {
  		//$url = 'http://www.someurl.com';
  		//$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
89
This Month
409
This Year
285

No Items Found.

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

This is my test area for webdev. I keep a collection of code here, mostly for my reference. Also if i find a good link, i usually add it here and then forget about it. more...

Subscribe to weekly updates about things i have added to the site or thought interesting during the last week.

You could also follow me on twitter or not... does anyone even use twitter anymore?

If you found something useful or like my work, you can buy me a coffee here. Mmm Coffee. ☕

❤️👩‍💻🎮

🪦 2000 - 16 Oct 2022 - Boots
Random Quote

james clear
Random CSS Property

animation-play-state

The animation-play-state CSS property sets whether an animation is running or paused.
animation-play-state css reference