0

我正在尝试使用Httpful PHP 库将我的数据发布到另一个 PHP 文件。

<?php
include("httpful.phar");
//use Httpful\Request;
//define('BASE_URL', 'http://localhost/StorePHP/server/php');

if(isset($_GET['action']))
    $action = $_GET['action'];
else
    exit(0);
echo "action =" .$action;
if($action == "Add")
{
    $name = $_GET['name'];
    $email = $_GET['email'];
    $msg = $_GET['msg'];

    //echo BASE_URL;

    //$url = 'http://localhost:8022/StorePHP/server/php/user_add.php';
    $url = 'http://localhost:8022/StorePHP/server/php/user_add';
    $response = \Httpful\Request::post($url) 
        ->sendsType(\Httpful\Mime::FORM)
        ->withoutStrictSsl()        // Ease up on some of the SSL checks
        ->expectsJson()  
        ->body('name='.$name.'&email='.$email.'&msg='.$msg)
        ->send();

    $resp_array = json_decode(json_encode($response->body),true);
    //echo $resp_array;

    if($resp_array['responseMessage']=='Success')
    {
        echo $resp_array['responseMessage'];
        return false;
    }

}

?>

我需要将数据发布到 user_add.php 文件中,然后将其插入数据库。

运行此代码时,我收到如下错误:

致命错误:第 30 行 phar://C:/wamp64/www/StorePHP/httpful.phar/Httpful/Handlers/JsonHandler.php 中的消息“无法将响应解析为 JSON”的未捕获异常“异常”异常:无法在第 30 行的 phar://C:/wamp64/www/StorePHP/httpful.phar/Httpful/Handlers/JsonHandler.php 中将响应解析为 JSON。

用户添加.php

 <?php
session_start();
include "config.php";
header('Content-Type: application/json');
$name = $_POST['name'];
$email = $_POST['email'];
$msg = $_POST['msg'];

if(true){
//$stmt = $conn->prepare("INSERT INTO users VALUES ('',?,?,?)");
    $stmt = $conn->prepare("INSERT INTO users (name, email, msg)
    VALUES (:name, :email, :msg)");



$stmt->bindParam(':name',$name,PDO::PARAM_STR);
$stmt->bindParam(':email',$email,PDO::PARAM_STR);
$stmt->bindParam(':msg',$msg,PDO::PARAM_STR);

//$conn->exec($stmt);
//$stmt -> execute();
//$stmt->bind_param('sss', $name, $email, $msg);

if($stmt->execute()){
    echo json_encode(array('responseMessage' =>'Success'));
    return false;
}

} 
?>

我得到的错误

4

0 回答 0