简体   繁体   English

PHP cURL POST请求不会发送?

[英]PHP cURL POST Request won't send?

I'm trying to update a mySQL database with cURL and PHP, but the values that are supposed to be sent by the post aren't inserted into the database. 我正在尝试使用cURL和PHP更新mySQL数据库,但是应该由帖子发送的值未插入数据库中。 I don't get any errors and cURL is enabled according to phpinfo(). 我没有收到任何错误,并且根据phpinfo()启用了cURL。 Here is my script: 这是我的脚本:

<?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);

?>

And here's the receiver script: 这是接收者脚本:

<?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]);
 }

?>

Looks like you are missing HTTP Auth for http://www.econcentre.com/receiver.php . 似乎您缺少http://www.econcentre.com/receiver.php的 HTTP身份验证。

You should include the HTTP Auth in the cURL request: 您应该在cURL请求中包括HTTP Auth:

<?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);

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM