【问题标题】:cURL not working (send/receive), php5-fpm and nginxcURL 不工作(发送/接收),php5-fpm 和 nginx
【发布时间】:2014-06-02 10:54:08
【问题描述】:

我最近的任务是创建一个可以完成几件事的脚本: - 解析来自 $_POST 的数据 - 将该数据插入本地数据库 - 将该数据作为 POST 发送给第三方

前两个目标很简单。获取数据,对其进行清理和验证,然后将其插入我的数据库。完成,完成,完成。

我遇到了第三个目标的问题。在查看了我的选项后,我确定 cURL 符合要求,但不幸的是,我无法让它发挥作用。我试过搜索 SO 和 Google,但我的搜索结果没有找到任何有用的东西。

这是我的“insert_data.php”文件。没有错误,没有失败,什么都没有。如果我手动为$data 定义一个数组,它就像一个魅力。

<?php
$data = $_POST;

$mysqli = new mysqli('localhost', 'root', 'password', 'database_name');

$key_list = implode(',', array_keys($data));
$val_list = implode(',', array_fill(0, count($data), '?'));
$types    = str_repeat('s', count($data));
$query    = "INSERT INTO leads ($key_list) VALUES ($val_list)";

if ( !$stmt = $mysqli->prepare($query) ) 
{
  die('connection error');
} else
{
  $stmt->bind_param($types, $data['first_name'], $data['last_name'], $data['email'])

  if ( !$stmt->execute() )
  {
    die('execution error');
  }
  else
  {
    die('success');
  }
}
?>

这是我的“send_curl.php”文件。我认为我的 cURL 使用有问题,但对于我的生活,我无法弄清楚。

<?php
$data = array(
  'first_name' => 'John',
  'last_name'  => 'Doe',
  'email'      => 'user@domain.com',
);

$url = 'insert_data.php';

$fields = '';
foreach($data as $key => $value) { 
  $fields .= $key . '=' . $value . '&'; 
}
rtrim($fields, '&');

$post = curl_init();
curl_setopt($post, CURLOPT_URL, $url);
curl_setopt($post, CURLOPT_POST, count($data));
curl_setopt($post, CURLOPT_POSTFIELDS, $fields);
curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($post);
curl_close($post);

if (isset($result))
{
  echo "<pre>";
  print_r($result);
  echo "</pre>";
}
else
{
  die('$result not set');
}
?>

我做错了什么?非常感谢您的帮助非常

【问题讨论】:

  • 尝试:$url = 'http://yoursite.com/insert_data.php'; - 使用绝对 URL
  • JakeGould 回复了同样的话;我赞成您的评论并接受他的帖子作为答案。非常感谢!

标签: php curl nginx mysqli


【解决方案1】:

这是在您的代码中,不是 URL:

$url = 'insert_data.php';

网址应该是这样的完整网址:

$url = 'http://my.great.site.here/insert_data.php';

有关如何处理curl_init URL 的更多详细信息,请访问the official PHP manual entry

【讨论】:

  • 你和@aldanux 是救生员,我快疯了。我认为这个简单的 api 是我不需要设置服务器块和主机覆盖来进行本地测试的东西......知道这是唯一的问题,我松了一口气。接受为答案。再次感谢!
猜你喜欢
  • 2016-06-11
  • 2016-01-08
  • 1970-01-01
  • 2016-02-24
  • 1970-01-01
  • 1970-01-01
  • 2012-12-05
  • 2014-07-05
  • 2013-08-28
相关资源
最近更新 更多