【问题标题】:PHP cURL POST Request won't send?PHP cURL POST 请求不会发送?
【发布时间】:2018-02-01 06:35:12
【问题描述】:

我正在尝试使用 cURL 和 PHP 更新 mySQL 数据库,但是应该通过帖子发送的值没有插入到数据库中。我没有收到任何错误,并且根据 phpinfo() 启用了 cURL。这是我的脚本:

<?php

  $data = [
    'email' => 'jsnow@got.com',
    'token' => '58938539'
  ];

$string = http_build_query($data);
$ch = curl_init("http://www.econcentre.com/receiver.php");

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_exec($ch);
curl_close($ch);

?>

这是接收器脚本:

<?php
   require 'includes/db.php';  // Database connection. Connects with no errors raised

   if(isset($_POST['email']) AND isset($_POST['token'])) {
     $email = $_POST['email'];
     $token = $_POST['token'];

     $q = "INSERT INTO reset_tokens(email, token)";
     $q .= " VALUES(?, ?)";
     $stmt = $pdo->prepare($q);
     $test = $stmt->execute([$email, $token]);
 }

?>

【问题讨论】:

  • exec() 之后检查 curl 错误了吗? if (curl_errno($ch)) { echo 'Curl error: ' . curl_error($ch); }
  • 感谢您的回复。我添加了你的 if 语句,但它没有回显错误。
  • 那么,您是否在接收器脚本中var_dump($_POST),在isset() 之前?

标签: php mysql curl post


【解决方案1】:

您似乎缺少 http://www.econcentre.com/receiver.php 的 HTTP 身份验证。

您应该在 cURL 请求中包含 HTTP 身份验证:

<?php

$data = [
    'email' => 'jsnow@got.com',
    'token' => '58938539'
];
$username = "auth-user";
$password = "auth-pwd";

$string = http_build_query($data);
$ch = curl_init("http://www.econcentre.com/receiver.php");

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);

$response = curl_exec($ch);
curl_close($ch);

?>

【讨论】:

    猜你喜欢
    • 2014-11-06
    • 2016-03-08
    • 1970-01-01
    • 2012-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-12
    • 2017-07-13
    相关资源
    最近更新 更多