gpt4 book ai didi

PHP 未从 Swift 接收 POST 数据

转载 作者:行者123 更新时间:2023-11-30 10:59:20 25 4
gpt4 key购买 nike

我正在尝试写入数据库,但无法将 POST 数据从 Swift 获取到 PHP。我已经检查了所有内容,并且在发送请求之前数据存在于 request.httpBody 中,但出现此错误“SQLSTATE[23000]:违反完整性约束:1048 列“order_id”不能为空”

PHP 还打印 $request 的空数组

感谢任何帮助。为这个绞尽脑汁!这是代码

环球银行金融电信协会:

    func saveNewProduct(product: ProductClass) {

//declare parameter as a dictionary which contains string as key and value combination.
let productDict: [String:Any] = ["order_id": product.order_id!,
"product_sku": product.product_sku!,
"product_description": product.product_description!,
"product_base_price": product.product_base_price!,
"product_price": product.product_price!,
"product_min_qty": product.product_min_qty!,
"product_qty": product.product_qty!,
"product_subtotal": product.product_subtotal!,
"product_imageURL": product.product_imageURL!]

let urlString: String = "https://www.mywebsite.com/dbinsertproduct.php"
guard let url = URL(string: urlString) else {
print("Error: cannot create URL")
return
}

//create the session object
let session = URLSession.shared

//create the URLRequest object using the url object
var request = URLRequest(url: url)
//set http method as POST
request.httpMethod = "POST"

do {
request.httpBody = try? JSONSerialization.data(withJSONObject: productDict, options: .prettyPrinted)
} catch let error {
print(error.localizedDescription)
}

request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")

let task = session.dataTask(with: request, completionHandler: {
data,response,error in

guard error == nil else {
return
}

guard let data = data else {
return
}

do {
//create json object from data
if let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
print(json)
// handle json...
}
} catch let error {
print(error.localizedDescription)
}
})
task.resume()
}

PHP:

    <?php include("dbclass.php"); ?>

<?php
$postdata = file_get_contents("php://input");
$request = json_decode($postdata, true);

$newProduct = new DBConnector('mysql.mysite.com', 'username', 'password', 'mysiteorders');
$p = $newProduct->dbInsertProduct($request);

error_log($p);
echo ($p);
?>

更多 PHP:

    public function dbInsertProduct($request)
{

try {

$stmt = $this->prepare("INSERT INTO `products` (`order_id`,`product_sku`,`product_description`,`product_base_price`,`product_price`,`product_notes`,`product_min_qty`,`product_qty`,`product_subtotal`,`product_imageURL`) VALUES (:order_id,:product_sku,:product_description,:product_base_price,:product_price,:product_notes,:product_min_qty,:product_qty,:product_subtotal,:product_imageURL);");

$stmt->bindParam(':order_id', $request['order_id']);
$stmt->bindParam(':product_sku', $request['product_sku']);
$stmt->bindParam(':product_description', $request['product_description']);
$stmt->bindParam(':product_base_price', $request['product_base_price']);
$stmt->bindParam(':product_price', $request['product_price']);
$stmt->bindParam(':product_notes', $request['product_notes']);
$stmt->bindParam(':product_min_qty', $request['product_min_qty']);
$stmt->bindParam(':product_qty', $request['product_qty']);
$stmt->bindParam(':product_subtotal', $request['product_subtotal']);
$stmt->bindParam(':product_imageURL', $request['product_imageURL']);

error_log( print_r($request, TRUE) );

$stmt->execute();

} catch(PDOException $e) {
error_log($e->getMessage());
echo json_encode($e->getMessage());
}
}

更新:

请求显示为 GET,而不是 POST(使用 Charles 代理验证)。正如您在代码中看到的,我正在设置 request.httpMethod = "POST"。我还漏掉了什么吗?

最佳答案

我发现从 www.mysite.com 重定向到 mysite.com 时,POST 被更改为 GET。删除“www”就成功了。

关于PHP 未从 Swift 接收 POST 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53583140/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com