我正在尝试使用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。
User_add.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;
}
}
?>