Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does php curl send get or post requests

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "how php curl sends get or post requests". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how php curl sends get or post requests".

Php can act as the sender of the data.

Simple get

$ch = curl_init (); curl_setopt ($ch, CURLOPT_URL, "http://SomeDomain/SamplePath?SomeVar=test");curl_exec($ch);curl_close($ch);"

two。 Simple post

$ch = curl_init (); curl_setopt ($ch, CURLOPT_URL, "http://SomeDomain/SamplePath");curl_setopt($ch, CURLOPT_POST, true); / / enable POSTcurl_setopt ($ch, CURLOPT_POSTFIELDS, http_build_query (array (" a "= >" 123", "b" = > "321")); curl_exec ($ch); curl_close ($ch)

3. Advanced post, which can set multiple parameters

$toURL = "http://SomeDomain/SamplePath?SomeVar=XX";$post = array (" a "= >" 123", "b" = > "321",); $ch = curl_init (); $options = array (CURLOPT_URL= > $toURL, CURLOPT_HEADER= > 0, CURLOPT_VERBOSE= > 0, CURLOPT_RETURNTRANSFER= > true, CURLOPT_USERAGENT= > "Mozilla/4.0 (compatible;)", CURLOPT_POST= > true, CURLOPT_POSTFIELDS= > http_build_query ($post),); curl_setopt_array ($ch, $options) / / CURLOPT_RETURNTRANSFER=true will return the server response code, and / / false will only return the success or failure $result = curl_exec ($ch); curl_close ($ch); echo $result

4. Post sends files

Sender code:

$target_url = 'https://cache.yisu.com/upload/information/20200310/52/110483.jpg'); / * curl will accept an array here too. * Many examples I found showed a url-encoded string instead. * Take note that the 'key' in the array will be the key that shows up in the * $_ FILES array of the accept script. And the at sign'@'is required before the * file name. * / $post = array ('extra_info' = >' 123456'. File_name_with_full_path); $ch = curl_init (); curl_setopt ($ch, CURLOPT_URL,$target_url); curl_setopt ($ch, CURLOPT_POST,1); curl_setopt ($ch, CURLOPT_POSTFIELDS, $post); curl_setopt ($ch, CURLOPT_RETURNTRANSFER,1) $result=curl_exec ($ch); curl_close ($ch); echo $result

Receiver code:

Uploaddir = realpath ('. /'). '/'; $uploadfile = $uploaddir. Basename ($_ FILES ['file_contents'] [' name']); echo''; if (move_uploaded_file ($_ FILES ['file_contents'] [' tmp_name'], $uploadfile)) {echo "File is valid, and was successfully uploaded.\ n";} else {echo "Possible file upload attack!\ n";} echo 'Here is some more debugging info:' Print_r ($_ FILES); echo "\ n\ n"; print_r ($_ POST); print "\ n"; these are all the contents of the article "how php curl sends get or post requests". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report