簡體   English   中英

php Curl無法在if語句中正確讀取響應,回顯正常

[英]php Curl failed to read response correctly in if statement, echos out fine

您好,我正在做我的大學工作,我們正在使用curl和php,我已經按要求制作了一個腳本。 該腳本從html表單中檢索一個id,然后使用curl將其傳遞給php腳本以更新數據庫,這一切正常,但是在我的curl腳本中,我從php頁面讀取了響應,該響應回顯

更新數據庫后,我的if語句無法讀取OK,我做錯了什么? 正如我所說的那樣,代碼運行良好,只是在讀取響應后在if語句中未顯示正確的消息,這是curl腳本在我嘗試后顯示的內容,再次更新了數據庫,只是沒有正確讀取響應而已。正如您在代碼中看到的那樣,我回顯了響應以對其進行測試,並且它顯示OK,在OK之前帶有一個空格,因此我嘗試了“ OK”和“ OK”,再次嘗試了一個空格,但是所有人都無法將其與響應匹配。

錯誤

測試響應= OK

編碼。

卷曲代碼

<?php
$id = $_POST["id"];

$connection = curl_init();
curl_setopt($connection, CURLOPT_URL, "http://southamptonstudentrooms.com/Wad/downloadwebservice3.php");
$dataToPost = array ("id" => $id);
curl_setopt($connection,CURLOPT_RETURNTRANSFER,1);
curl_setopt($connection,CURLOPT_POSTFIELDS,$dataToPost);
$response = curl_exec($connection);
// display any errors
if(curl_errno($connection)){
    echo 'Curl error: ' . curl_error($ch);
}
curl_close($connection);
if ($response == " OK") {
    echo "updated";
    }
else if ($response == " ERROR") {
    echo "updated failed";
    }
else {
    echo "error";
    }
echo "<br><br>test response =" . $response; // displays " OK" 
var_dump($response); // displays: string(4) " OK"
?>

它訪問的PhP腳本-要連接的用戶信息已更改,以確保安全

<?php
$servername = "localhost";
$username = "south609_edwin";
$password = "Zx10r2006";
$dbname = "south609_test_database";
$ID = $_POST["id"];
$status;

    // 11: Chris Palmer, username ChrisPalmer, Password tree987, Balance 2.00
    // Create connection
    $conn = mysqli_connect($servername, $username, $password, $dbname);
    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }

    $sql = "SELECT * FROM ht_users WHERE username='ChrisPalmer'";
    $result = mysqli_query($conn, $sql);

    if (mysqli_num_rows($result) > 0) {
        // output data of each row
        while($row = mysqli_fetch_assoc($result)) {
            if($row["balance"] >= 0.79)
            {
                mysqli_close($conn);
                try {
                    //WAI
                    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
                    // set the PDO error mode to exception
                    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

                    $sql = "UPDATE wadsongs SET downloads=downloads + 1 WHERE songid='$ID'";

                    // Prepare statement
                    $stmt = $conn->prepare($sql);

                    // execute the query
                    $stmt->execute();

                    $status = "OK";
                    // echo a message to say the UPDATE succeeded
                    // echo $stmt->rowCount() . " records UPDATED successfully";
                    }
                    catch(PDOException $e)
                    {
                        echo $sql . "<br>" . $e->getMessage();
                    }
                $conn = null;
            }
            else{
                $status = "ERROR";
            }
        }
    }
    else {
        echo "0 results";
        mysqli_close($conn);
    }
echo $status;
?>

您遇到的問題是您得到的響應中有一個空格與“ OK”不匹配,因此您可以借助PHP中的trim()函數來修剪空白

所以現在您的代碼將成為

if(trim($response) == "OK"){
//Your success code
}

就這么簡單,謝謝Channaveer Hakari。 即使我試圖讀取空格並輸入字符串,它仍然奇怪地失敗了,但是您的修剪想法非常有效,現在可以按預期工作了。 如果有人知道為什么php會增加空格,就像C一樣,在字符流等情況下傳遞時,它會添加一個空指針/ 0。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM