英文:
How Should I Get Data From My Own Webpage Using Go?
问题
我想要使用Go程序来格式化我网站上的数据。目前我的网站是用PHP编写的,我正在从一个SQL表中获取数据。我的网站代码如下:
<?php
include("connection.php");
$sql = mysqli_query($con, "SELECT * FROM talk");
while ($info = $sql->fetch_assoc()) {
echo "Name: " . $info['name'];
echo "<br>";
echo "Message: " . $info['message'];
echo "<br>";
}
当前页面显示的内容如下:
Name: John
Message: My name is John
Name: Doe
Message: My name is Doe
我希望能够使用Go脚本来获取姓名和消息的信息。我可以修改网站的代码,我只是简单地编写了它,以便能够简要了解我想要做什么。
英文:
I want to be able to format data from my website using a Go program. Right now my website is written in PHP and I am fetching data from an SQL table. My websites code is
<?php
include("connection.php");
$sql = mysqli_query($con, "SELECT * FROM talk");
while ($info = $sql->fetch_assoc()) {
echo "Name: " . $info['name'];
echo "<br>";
echo "Message: " . $info['message'];
echo "<br>";
}
And the page currently displays
Name: John
Message: My name is John
Name: Doe
Message: My name is Doe
I would like to be able to use a Go script to fetch the information of what the name and message is. I am open to changing the code for my website and I wrote it very simply to be able to give a brief understanding of what I am trying to do.
答案1
得分: 2
我建议以下两个步骤:
步骤1:PHP代码生成纯文本(例如:"John;Message 1\n;Doe;Message2\n")(参见这里:PHP - 渲染纯文本)
步骤2:在GO中,使用HTTP GET请求从PHP服务器获取信息:
package phpgo
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
)
func getFromPhp(url string, filename string) ([]string, error) {
var err error
fmt.Printf("Downloading %s...\n", filename)
// 执行HTTP GET请求
resp, err := http.Get(url)
if err != nil || resp.StatusCode != 200 {
err = fmt.Errorf("从 %s 下载失败", url)
return nil, err
}
// 读取响应体的内容
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil || resp.StatusCode != 200 {
err = fmt.Errorf("从 %s 下载失败", url)
return nil, err
}
// 将数据的每个元素放入切片中
infos := strings.Split(string(body), "\n")
return infos, nil
}
英文:
I would suggest these two steps :
STEP 1 : PHP code produces plain text (eg : "John;Message 1\n;Doe;Message2\n") (see here : PHP - Render plain texthttps://stackoverflow.com/questions/4506679/rendering-plain-text-through-php)
STEP 2 : In GO, use http GET to request the infos from the PHP server:
package phpgo
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
)
func getFromPhp(url string, filename string) ([]string, error) {
var err error
fmt.Printf("Downloading %s...\n", filename)
//Execute the HTTP GET request
resp, err := http.Get(url)
if err != nil || resp.StatusCode != 200 {
err = fmt.Errorf("failed download from %s ", url)
return err
}
//Read the content the from response body
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil || resp.StatusCode != 200 {
err = fmt.Errorf("failed download from %s ", url)
return err
}
//Put each element of data in a slice
infos := strings.Split(string(body), "\n")
return infos, err
}
1: https://stackoverflow.com/questions/4506679/rendering-plain-text-through-php)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论