cURL POST 数组导致为 foreach() 提供的参数无效

问题描述 投票:0回答:1

我正在尝试使用 cURL 将一组数据从一个域发布到另一个域。当我执行下面的代码时,我收到错误消息“为 foreach() 提供的参数无效”。我怀疑“json_decode”之后的数组不正确,或者我在发送脚本中没有以正确的方式形成它?

最终我只想将一组值从一个域传递到另一个域以插入到 mysql 数据库中。当然,一旦这个基本功能发挥作用,我将锁定 PDO 并实施令牌。

这是我的发送脚本

<?php

// Create an array of rows to encode, using the field names we want
$array_data = array (
  array("name"=>"TestArr1","desc"=>"one","price"=>100),
  array("name"=>"TestArr2","desc"=>"two","price"=>200),
  array("name"=>"TestArr3","desc"=>"three","price"=>300)
);           
            
// Encode data to a JSON packet
$json_data = json_encode($array_data);  

$ch = curl_init("https://www.receivingdomain.com/test_insert_multiple.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$array_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

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

$data = json_decode($response, true);

?>

这是我的接收脚本

<?php 

 // Retrieve the raw POST data
$jsonData = file_get_contents('php://input');

// Decode the JSON data into a PHP associative array
$data = json_decode($jsonData, true);

require ("/home/test/cnct.php");

 /* insert data into DB */
    foreach($data as $row) {
        $sql = "INSERT INTO `products` (`id`, `name`, `description`, `price`, `category_id`, `created`, `modified`) VALUES (NULL, '" . $row["name"] . "', '" . $row["desc"] . "', '" . $row["price"] . "', '2', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)"; 
        $stmt = $conn->prepare($sql);   
        $stmt->execute();
     }
     
  //close the connection
    $stmt = null;
    $conn = null; 

echo json_encode(array("message" => "Products were created."));

?>

任何帮助将不胜感激。

php arrays json loops
1个回答
0
投票

我不会详细说明应该以不同方式完成的所有事情

<?php

// Create an array of rows to encode, using the field names we want
$array_data = array (
  array("name"=>"TestArr1","desc"=>"one","price"=>100),
  array("name"=>"TestArr2","desc"=>"two","price"=>200),
  array("name"=>"TestArr3","desc"=>"three","price"=>300)
);           
            
// Encode data to a JSON packet
$json_data = json_encode($array_data);  

$ch = curl_init("https://www.receivingdomain.com/test_insert_multiple.php");
curl_setopt($ch, CURLOPT_POST, 1);
//#############################################################
//here  is your first mistake... if you encode into $json_data 
//i suppose you want to send a json string
//but you are sending the original array($array_data):
//#############################################################
//curl_setopt($ch, CURLOPT_POSTFIELDS,$array_data);


//you need to make your string clean , i use  base64_encode but urlencode can be used too
curl_setopt($ch, CURLOPT_POSTFIELDS,base64_encode($json_data));

//this is for send text/plain raw data
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type: text/plain')); 
//=====================================================================
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

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

$data = json_decode($response, true);

?>

在另一边你只需要像这样添加base64_decode:

<?php 

 // Retrieve the raw POST data
$jsonData = file_get_contents('php://input');

// Decode the JSON data into a PHP associative array
$data = json_decode(base64_decode($jsonData), true);
//....
© www.soinside.com 2019 - 2024. All rights reserved.