Skip to content

Instantly share code, notes, and snippets.

@kenial
Last active August 16, 2016 18:10
Show Gist options
  • Save kenial/610b36cf44b201d4397e7e1628a60c61 to your computer and use it in GitHub Desktop.
Save kenial/610b36cf44b201d4397e7e1628a60c61 to your computer and use it in GitHub Desktop.
Mix and match POST request w/ setting querystring, cookie, and response header in PHP
<?php
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
?>
<?php
//
// A very simple PHP example that sends a HTTP POST to a remote site
//
$post_data = [
'username' => 'user1',
'password' => 'passuser1&pass',
'gender' => 1,
];
$get_data = [
'getdata1' => 'test&test',
'getdata2' => 2,
];
$header_data = [
'X-Apple-Tz: 0',
'X-Apple-Store-Front: 143444,12',
];
$cookie_data = 'user=ellen; activity=swimming&singing';
$url = "https://www.test.com/test.php"; // endpoint
$url = $url."?".http_build_query($get_data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_data);
curl_setopt($ch, CURLOPT_COOKIE, $cookie_data);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
// execute!
$response = curl_exec($ch);
// close the connection, release resources used
curl_close($ch);
// do anything you want with your response
print_r($response);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment