Overview
I have a script, we'll call it one.php, that creates a database and tables. It also contains an array of data to be posted to another script, two.php, which will sort through the data and insert it into our newly created database.
Your help is much, much appreciated.
The Problem
two.php has a check for the $_POST[] array at the very top of the script:
if (empty($_POST))
{
$response = array('status' => 'fail', 'message' => 'empty post array');
echo json_encode($response);
exit;
}
Normally, this would not be triggered unless the post array is, well, empty(). However, when sending the data from one.php to two.php via cURL, I'm receiving the above encoded array as my response, and my data does not progress further down two.php.
I'll lay out the relevant code from the files below for your viewing pleasure:
one.php
$one_array = array('name' => 'John', 'fav_color' => 'red');
$one_url = 'http://' . $_SERVER['HTTP_HOST'] . '/path/to/two.php';
$response = post_to_url($one_url, $one_array, 'application/json');
echo $response; die;
This is currently giving me the below:
{"status":"fail","message":"empty post array"}
The post_to_url() function, for reference
function post_to_url($url, $array, $content_type)
{
$fields = '';
foreach($array as $key => $value)
{
$fields .= $key . '=' . $value . '&';
}
$fields = rtrim($fields, '&');
$ch = curl_init();
$httpheader = array(
'Content-Type: ' . $content_type,
'Accept: ' . $content_type
);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
two.php
header("Content-type: application/json");
$response = array(); //this is used to build the responses, like below
if (empty($_POST))
{
$response['status'] = 'fail';
$response['message'] = 'empty post array';
echo json_encode($response);
exit;
}
elseif (!empty($_POST))
{
//do super neat stuff
}
curl_setopt($ch, CURLOPT_POST, 1);true?$fieldsstring look like after the foreach loop and rtrim?$fields = 'name=John&fav_color=red'$httpheaderif you look closely, there are two words in this identifier, http and header, but you didn't separate them. You should do$http_header