天气API:如何在php中编写“ $ .ajax部分”

早上好。昨天我编写了这段代码,它使用开源api获取由用户编写的城市的天气情况(http://api.openweathermap.org/)。

<head>
<script src="js.js"></script>

        <script>

        $(document).ready(function(){


            $("#submit").click(function (e) {


            $.ajax({
                type:"POST",


                url:"http://api.openweathermap.org/data/2.5/weather?q="+$("#city").val() +"&units=metric&appid=bebaab124fa9097086191ffc72ded353&lang=en" ,
                 dataType: "json",
                 success: function(result,status,xhr) {
                    console.log(result);
                    var list = $("<table><tr><th>Meteo</th></tr>");
                    list.append("<tr><td>City:</td><td>" + result["name"]+ "</td></tr>");
                    list.append("<tr><td>Country:</td><td>" + result["sys"] ["country"]+ "</td></tr>");
                    list.append("<tr><td>Temperature:</td><td>" + result["main"] ["temp"]+ " C</td></tr>");
                    list.append("<tr><td>Humidity:</td><td>" + result["main"] ["humidity"]+ "</td></tr>");
                    list.append("<tr><td>Weather:</td><td>" + result["weather"][0]["description"]+ "</td></tr>");
                    list.append("</table>");

                    $("#list").html(list);
                 } , 
                 error: function(xhr , status , error) {
                        alert("result: " + status + " " + error + " "+ xhr.status + " " + xhr.statusText)
                 }
            });
          });

        });

</script>

<body>
    <h1>Write your city</h1>

    <input type="text" id="city" name="city"><br><br>

    <button type="submit" id="submit" onclick="">Send</button>

    <div id="list"></div>

</body>

如何将该脚本“Traslate”到PHP中? 我不知道该怎么做......我已经准备好了带有文本输入的表单页面,用户可以在其中输入城市,但是......我不知道该如何在php中编写“ $ .ajax部分”。

参考GPT和自己的思路:

你好!针对你的问题,我可以给你一些指导:

首先,需要注意的是,PHP和jQuery的语法是不同的,因此需要根据PHP的语法来重写你的代码。以下是一个范例:

```
<?php
if(isset($_POST['city'])) {
$city = $_POST['city'];
$url = "http://a/