Php cURL没有实际发送POST数据

Php cURL没有实际发送POST数据,php,post,curl,Php,Post,Curl,概述 我有一个脚本,我们称之为one.php,它创建一个数据库和表。它还包含一个要发布到另一个脚本two.php的数据数组,该脚本将对数据进行排序并将其插入我们新创建的数据库中 非常感谢您的帮助。 问题 two.php在脚本的最顶端检查了$\u POST[]数组: if (empty($_POST)) { $response = array('status' => 'fail', 'message' => 'empty post array'); echo json_enco

概述
我有一个脚本,我们称之为
one.php
,它创建一个数据库和表。它还包含一个要发布到另一个脚本
two.php
的数据数组,该脚本将对数据进行排序并将其插入我们新创建的数据库中

非常感谢您的帮助。

问题
two.php
在脚本的最顶端检查了
$\u POST[]
数组:

if (empty($_POST))
{
  $response = array('status' => 'fail', 'message' => 'empty post array');
  echo json_encode($response);
  exit;
}
通常,除非post数组是空的()。但是,当通过
cURL
将数据从
one.php
发送到
two.php
时,我将接收上面的编码数组作为响应,并且我的数据不会进一步向下移动
two.php

我将从以下文件中列出相关代码,供您查看:

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;
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;
}
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
}
这给了我以下信息:


用于参考的
post\u to\u url()
函数

$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;
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;
}
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
}

two.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;
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;
}
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
}

因为您将请求正文内容类型设置为“application/json”,所以PHP不会在“two.PHP”中填充
$\u POST
。因为您正在发送url编码的数据,所以最好只发送
Accept:
标题:

curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: ' . $content_type]);
也就是说,“two.php”实际上并不使用Accept:header并始终输出JSON;在这种情况下,您可以完全不设置
CURLOPT_HTTPHEADER

更新 从数组创建url编码的数据也可以更简单(更安全):


我有一些这样的问题,但在我的情况下,我补充说

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

问题解决了。

CURLOPT\u POST应该是true或false,而不是计算您要发布多少内容,true将该行更改为
curl\u setopt($ch,CURLOPT\u POST,1)
@iamde_coder,很好,谢谢-但这并不能解决问题。旁注:奇怪的是,count($array)在以前的脚本中对我起了作用。也许任何1+返回的值都是
真的
?很好,是的。完成的
$fields
字符串在foreach循环和rtrim之后是什么样子的?使用上面的示例数组,
$fields='name=John&fav\u color=red'
$httpheader
如果仔细看,这个标识符中有两个词,http和header,但没有将它们分开。您应该执行
$http\u header
我不确定您是否正确阅读了我的问题。我正在将表单提交的数据发布到脚本中,然后脚本使用cURL将其发布到另一个脚本中。@Benjamin我的答案现在清楚了吗?看来你接受它只是为了接受。。。否则,如果有什么不清楚的地方,一定要告诉我。我去吃点东西,但你在第一句话中突出了这个问题。我编辑你的文章是为了清晰明了,以防其他人将来犯我犯的同样的错误。谢谢你的帮助!http\u build\u query()帮助。谢谢