11

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
}
12
  • CURLOPT_POST should be true or false, not a count of how many things you want to post, true changing that line to curl_setopt($ch, CURLOPT_POST, 1); Commented Sep 19, 2014 at 2:54
  • @iamde_coder, Good catch, thanks - but that does not resolve the issue. Side-note: Curiously though, the count($array) has worked for me on previous scripts. Perhaps anything 1+ returns as true? Commented Sep 19, 2014 at 2:58
  • pretty much, ya. what does your completed $fields string look like after the foreach loop and rtrim? Commented Sep 19, 2014 at 3:00
  • Using the example array above, $fields = 'name=John&fav_color=red' Commented Sep 19, 2014 at 3:06
  • $httpheader if you look closely, there are two words in this identifier, http and header, but you didn't separate them. You should do $http_header Commented Sep 19, 2014 at 3:07

2 Answers 2

9

Because you're setting the request body content type as "application/json", PHP will not populate $_POST in "two.php". Because you're sending url encoded data, the best thing to do is only send the Accept: header:

curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: ' . $content_type]);

That said, "two.php" doesn't actually use the Accept: header and always outputs JSON; in that case, you can make do with not setting CURLOPT_HTTPHEADER at all.

Update

Creating url encoded data from an array can be simpler (and safer) too:

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($array));
Sign up to request clarification or add additional context in comments.

4 Comments

I'm not sure you read my question properly. I'm posting data submitted from a form to a script, which is then using cURL to post it to another script.
@Benjamin Is my answer clear now? Seems that you just accepted it for the sake of accepting ... otherwise, do let me know if there's anything unclear about it.
I went to go grab a bite to eat, but you had the issue highlighted in the first sentence. I edited your post for clarity in case others make the same mistake I did in the future. Thanks for your help!
http_build_query() helped. Thanks !
0

I have some issue like that, but in my case I added

Content-Type: {APPLICATION/TYPE}
Content-Length: {DATA LENGTH}

and problem was solved.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.