PHP创建Knack数据库记录失败求助,JS版本可正常运行
Hey folks,
I posted about this a little while back and didn't get any traction, but since I've made some progress, I figured I'd circle back for help.
I've got a fully functional JavaScript script that calls the Knack API to create records in my database—no issues there at all. Now I'm trying to port that same functionality over to PHP, and I'm hitting a wall. You don't need to know anything about Knack specifically; I just need someone who knows PHP well to help me spot where my conversion might be off.
Here's the JavaScript code that works perfectly:
async function createKnackRecord() { const apiUrl = "https://api.knack.com/v1/objects/object_2/records"; const response = await fetch(apiUrl, { method: "POST", headers: { "X-Knack-Application-Id": "MY_APP_ID", "X-Knack-REST-API-Key": "MY_API_KEY", "Content-Type": "application/json" }, body: JSON.stringify({ field_3: "Test Entry", field_4: "2024-05-20" }) }); const result = await response.json(); console.log("Record created:", result); } createKnackRecord();
And here's my PHP code that's failing to create records (and isn't giving me a clear error message to debug):
$apiUrl = "https://api.knack.com/v1/objects/object_2/records"; $headers = array( "X-Knack-Application-Id: MY_APP_ID", "X-Knack-REST-API-Key: MY_API_KEY", "Content-Type: application/json" ); $postData = array( "field_3" => "Test Entry", "field_4" => "2024-05-20" ); $ch = curl_init($apiUrl); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData)); $response = curl_exec($ch); $curlError = curl_error($ch); curl_close($ch); if ($curlError) { echo "Curl Error: " . $curlError; } else { $responseData = json_decode($response, true); print_r($responseData); }
I've double-checked the API keys, object IDs, and field names to make sure they match exactly between the JS and PHP code, but something's still not right. Any ideas where I might be messing up?
内容的提问来源于stack exchange,提问作者sk bidderman

