[Help] POST Examples PHP Side?

agolangf · · 729 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Looking for a Go POST example, I want to send data to a PHP file via POST (lots of data)... There aren&#39;t any examples that i could find... </p> <p>If possible include the PHP side of it?</p> <hr/>**评论:**<br/><br/>anoland: <pre><p>This may help: <a href="http://nathanleclaire.com/blog/2013/11/30/fear-and-loathing-with-golang-and-angular-js/" rel="nofollow">http://nathanleclaire.com/blog/2013/11/30/fear-and-loathing-with-golang-and-angular-js/</a></p> <p>One thing that PHP glosses over is that a post is actually in the http body. In Go you have to read the body then decode/unmarshal that. </p></pre>ecmdome: <pre><p><a href="https://golang.org/pkg/net/http/#Post" rel="nofollow">https://golang.org/pkg/net/http/#Post</a></p></pre>SaturnsVoid: <pre><p>Obviously i have seen this. But it does nothing but tell me its possible to do in Go. I need for information, examples to learn from. </p></pre>ecmdome: <pre><p>How is it obvious that you have seen this if you asked such a question?</p> <p>Go is a very idiomatic language, reading the libraries are pretty self explanatory. If you would have looked at the very top of that page I sent you, you would have seen a POST example with values, but I will try to break it down yo you further.</p> <p>You are looking to use the http.PostForm function most likely, this will make it easiest to send a post request. You can also do this by creating a NewRequest which gives more flexibility but we will stick to the simple one for now.</p> <p>This function takes 2 arguments, a string which represents your endpoint (the php file&#39;s address), and a url.Values data type which holds all of that post data.</p> <p>If you look at url.Values(which you can click in the go documentation) you will see that it&#39;s simple a map[string][]string</p> <p>in response you get a reponse, which you can also click the type and get all of the information you might need about it. and you get an error.</p> <p>Now in the response, you&#39;re interested in the Body, which is a ReadCloser type, basically a readier. We will use io/ioutil to read this very quickly.. you can also use bufio to buffer the response(if you&#39;re expecting really long responses this might be best, but it&#39;s doubtful w/ php)</p> <p>Now knowing all of that information here is some example code that will do just this. I would have linked it in the Go Playground but it restricts socket requests(rightfully so)</p> <pre><code>package main import ( &#34;fmt&#34; &#34;io/ioutil&#34; &#34;log&#34; &#34;net/http&#34; ) func main() { url := &#34;http://requestb.in/yfc4fbyf&#34; data := url.Values{ &#34;key&#34;: {&#34;Value&#34;}, &#34;id&#34;: {&#34;123&#34;}, } resp, err := http.PostForm( url, data, ) if err != nil { log.Println(&#34;post err&#34;) panic(err) } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { log.Println(&#34;ioutil err&#34;) panic(err) } fmt.Println(string(body)) } </code></pre> <p>Now this may or may not work for your data-type of input. I recommend after fiddling a bit with something like this that you look at forming your own request with http.NewRequest, you then have full reign to make &#34;POST&#34;,&#34;PUT&#34;,&#34;GET&#34;,&#34;PATCH&#34; etc requests, add headers, and include more versatile data types.</p> <p>edit: reddit kind of chopped up my code</p></pre>SaturnsVoid: <pre><p>I apologize for my earlier words. I have looked at your example and tested it but it gives me the following error;</p> <blockquote> <p>M:\Computer\Programming\Go\HTTP POST.go:13: url.Values undefined (type string has no field or method Values)</p> </blockquote> <p>From what i can tell, with my limited knowledge of Go, it should be working... But its not. I am able to understand how it should do the job though, so thank you.</p></pre>schoenobates: <pre><p>My guess is you&#39;ve shadowed a net url import with a string variable called url...</p></pre>jasonrichardsmith: <pre><p>I suggest you hire a contractor.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

729 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传