你应该如何使用Go从自己的网页获取数据?

huangapple go评论91阅读模式
英文:

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

&lt;?php
include(&quot;connection.php&quot;);
$sql = mysqli_query($con, &quot;SELECT * FROM talk&quot;);
while ($info = $sql-&gt;fetch_assoc()) {
    echo &quot;Name: &quot; . $info[&#39;name&#39;];
    echo &quot;&lt;br&gt;&quot;;
    echo &quot;Message: &quot; . $info[&#39;message&#39;];
    echo &quot;&lt;br&gt;&quot;;
}

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(&quot;Downloading %s...\n&quot;, filename)

//Execute the HTTP GET request
resp, err := http.Get(url)
if err != nil || resp.StatusCode != 200 {
	err = fmt.Errorf(&quot;failed download from %s &quot;, 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(&quot;failed download from %s &quot;, url)
	return err
}

//Put each element of data in a slice
infos := strings.Split(string(body), &quot;\n&quot;)

return infos, err

}

1: https://stackoverflow.com/questions/4506679/rendering-plain-text-through-php)

huangapple
  • 本文由 发表于 2021年12月17日 08:25:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/70387132.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定