Php JSON/CURL问题
我的CURL请求包含两个变量,如下所示:Php JSON/CURL问题,php,json,curl,Php,Json,Curl,我的CURL请求包含两个变量,如下所示: <?php $id = $_GET["id"]; $review = $_GET["review"]; $url = 'http://edward/~treeves/ratingJSON.php'; $jsonData = array("id" => "$id", "review" => "$review"); $ch = curl_init($url); $json
<?php
$id = $_GET["id"];
$review = $_GET["review"];
$url = 'http://edward/~treeves/ratingJSON.php';
$jsonData = array("id" => "$id", "review" => "$review");
$ch = curl_init($url);
$jsonDataEncoded = json_encode($jsonData);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('json' => $jsonDataEncoded));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
?>
<?php
header("Content-type: application/json");
try {
$conn = new PDO("mysql:host=localhost;dbname=treeves;", "treeves", "oopheiye");
$json = $_POST["json"];
$data = json_decode ($json, true);
$result = $conn->query("INSERT INTO poi_reviews (poi_id, review)
VALUES ('$data[ID]', '$data[review]')");
} catch (Exception $e) {
echo $e->getMessage();
}
?>
在这里,您使用一个键(json)设置表单编码数据,该键具有一个值(json编码数据的值): 在这里,您告诉服务器您正在发送JSON,而不是表单编码的数据
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
上述其中一项肯定是错误的,因为它们相互矛盾
在这里,您尝试读取表单编码数据:
curl_setopt($ch, CURLOPT_POSTFIELDS, array('json' => $jsonDataEncoded));
$_POST["json"];
因此,声称您发送的是纯JSON是错误的(您的JSON被封装在表单编码中)
删除此行:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
您是否尝试在Web服务中解码后立即使用
json\u last\u error()
?另外,您的$\u POST
变量的内容是什么?它返回'NULL bool(false)'尝试删除httpheader?返回'array(2){[“id”]=>string(4)“id”[“review”]=>string(1)“review”}bool(false)'为什么不单独测试系统的一侧?这不需要是PHP问题、curl问题和JSON问题。
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));