How to convert curl to php
How to convert curl to php: 1. Get the status through "curl -X GET -H "Content-Type:application"..."; 2. Set the status; 3. Through "$header= array( ...)" method can be used to convert curl to php and send it.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
How to convert curl to php?
Convert curl command to php source code
Get status:
curl -X GET -H "Content-Type:application/json" -H "Authorization: token 4e56266f2502936e0378ea6a985dc74a5bec4280" http://user.endv.cn/v1/datastreams/plug-status/datapoint/
Return
{"status": 200, "datapoint": null}
Set status
curl -H "Authorization: token 6bcb3cdb69b07370f5ad73e7a856409802fdd735" -d "{\"datapoint\":{\"x\":1}}" http://user.endv.cn/v1/datastreams/plug-status/datapoint/?deliver_to_device=true
Return
{"status": 404, "nonce": 333984364, "message": "remote device is disconnect"}
curl to php and send
Get status:
Set status:
Use The json data sent by php curl is the same as other data in curl post.
Let me summarize a few examples of json data sent by curl post.
Example 1
$data = array("name" => "Hagrid", "age" => "36"); $data_string = json_encode($data); $ch = curl_init('http://api.local/rest/users'); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($data_string)) ); $result = curl_exec($ch);
Example 2
function http_post_data($url, $data_string) { $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json; charset=utf-8', 'Content-Length: ' . strlen($data_string)) ); ob_start(); curl_exec($ch); $return_content = ob_get_contents(); ob_end_clean(); $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); return array($return_code, $return_content); } $url = "http://xx.xx.cn"; $data = json_encode(array('a'=>1, 'b'=>2)); list($return_code, $return_content) = http_post_data($url, $data);
Example 3
$data=' { "button":[ { "type":"click", "name":"今日歌曲", "key":"V1001_TODAY_MUSIC" }, { "type":"click", "name":"歌手简介", "key":"V1001_TODAY_SINGER" }, { "name":"菜单", "sub_button":[ { "type":"click", "name":"hello word", "key":"V1001_HELLO_WORLD" }, { "type":"click", "name":"赞一下我们", "key":"V1001_GOOD" }] }] }'; $ch = curl_init($urlcon); //请求的URL地址 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//$data JSON类型字符串 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data))); $data = curl_exec($ch); print_r($data);//创建成功返回:{"errcode":0,"errmsg":"ok"}
curl post sending and receiving
<?php $url = "http://localhost/web_services.php"; $post_data = array ("username" => "bob","key" => "12345"); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // post数据 curl_setopt($ch, CURLOPT_POST, 1); // post的变量 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); $output = curl_exec($ch); curl_close($ch); //打印获得的数据 print_r($output); ?>
<html> <body> Welcome <?php echo $_POST["username"]; ?>.<br /> You are <?php echo $_POST["key"]; ?> years old. </body> </html>
get test
//curl -X GET -H "Content-Type:application/json" -H "Authorization: token 4e56266f2502936e0378ea6a985dc74a5bec4280" http://user.endv.cn/v1/datastreams/plug-status/datapoint/ $url = "http://localhost/web_services.php"; $post_data = array ("username" => "bob","key" => "12345"); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); //打印获得的数据 print_r($output);
A brief introduction to the use of curl
Curl is a very powerful http command line tool under Linux, and its functions are very powerful.
1) Without further ado, let’s start from here!
$ curl http://code.endv.cn
After pressing Enter, the html of code.endv.cn will be displayed on the screen~
2) Well, if you want to save the page you have read, should you do this? Woolen cloth?
$ curl http://code.endv.cn > page.html
Of course you can, but it doesn’t have to be so troublesome!
Just use curl's built-in option. To save the http results, use this option: -o
$ curl -o page.html http://code.endv.cn
In this way, you can see a download page progress indicator appear on the screen. When the progress reaches 100%, it will be OK
3) What? ! Can’t access? It must be that your proxy is not configured.
When using curl, you can use this option to specify the proxy server and port used for http access: -x
$ curl -x 123.45.67.89:1080 -o page.html http://code.endv.cn
4) It is annoying when visiting some websites. They use cookies to record session information.
Browsers like IE/NN can certainly handle cookie information easily, but what about our curl? .....
Let’s learn this option: -D <— This is to save the cookie information in the http response into a special file
$ curl -x 123.45.67.89:1080 -o page.html -D cookie0001.txt http://code.endv.cn
In this way, when When the page is saved to page.html, the cookie information is also saved to cookie0001.txt
5) So, how to continue to use the cookie information left last time the next time you visit? You know, many websites rely on monitoring your cookie information to determine whether you are visiting their website in violation of the rules.
This time we use this option to append the last cookie information to the http request: -b
$ curl -x 123.45.67.89:1080 -o page1.html -D cookie0002.txt -b cookie0001.txt http://code.endv.cn
In this way, we can simulate almost all IE operations to access the web page !
6) Wait a moment~I seem to have forgotten something~
That’s right! It’s browser information
Some annoying websites always require us to use certain specific browsers to access them. Sometimes, what’s more, we have to use certain specific versions of NND. Where do we have time for it? Go find these weird browsers! ?
Fortunately, curl provides us with a useful option, which allows us to arbitrarily specify the browser information we declare for this visit: -A
$ curl -A "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" -x 123.45.67.89:1080 -o page.html -D cookie0001.txt http://code.endv.cn
In this way, the server receives When requesting access, you will be considered to be an IE6.0 running on Windows 2000. Hey, hey, in fact, maybe you are using a Mac!
And "Mozilla/4.73 [en] (X11; U; Linux 2.2; 15 i686" can tell the other party that you are running Linux on a PC and using Netscape 4.73, hahaha
7) Another commonly used restriction method on the server side is to check the referer for http access. For example, if you visit the homepage first, and then visit the download page specified there, the referer address of the second visit will be the page address after the first successful visit. In this way, as long as the server finds that the referer address of a certain visit to the download page is not the address of the home page, it can conclude that it is a stolen connection ~
hate hate~I just want to steal the connection~! !
Fortunately, curl provides us with the option to set the referer: -e
$ curl -A "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" -x 123.45.67.89:1080 -e "mail.linuxidc.com" -o page.html -D cookie0001.txt http://code.endv.cn
In this way, you can deceive the other party's server. You clicked a link from mail.linuxidc.com. , Hahaha
8) As I write, I find that I have missed something important! ——-Use curl to download files
As I just said, you can use -o to download a page into a file, and the same is true for downloading files. For example,
$ curl -o 1.jpg http://img.endv.cn/~zzh/screen1.JPG
Here we teach you a new option: -O capital O, use it like this:
$ curl -O http://img.endv.cn/~zzh/screen1.JPG
In this way, you can automatically save it locally according to the file name on the server!
One more useful one.
If in addition to screen1.JPG there are screen2.JPG, screen3.JPG, ...., screen10.JPG that need to be downloaded, is it possible that we need to write a script to complete these operations?
Don’t do it!
In curl, just write like this:
$ curl -O http://img.endv.cn/~zzh/screen[1-10].JPG
Hahaha, isn’t it awesome? ! ~
9) Come again, let’s continue to explain downloading!
$ curl -O http://img.endv.cn/~{zzh,nick}/[001-201].JPG
The download generated in this way is
~zzh/001.JPG ~zzh/002.JPG ... ~zzh/201.JPG ~nick/001.JPG ~nick/002.JPG ... ~nick/201.JPG
convenient enough, right? Hahaha
Eh? It's too early to be happy.
Since the file names under zzh/nick are all 001, 002..., 201, the downloaded files have the same name, and the later ones overwrite the previous files ~
没关系,我们还有更狠的!
$ curl -o #2_#1.jpg http://img.endv.cn/~{zzh,nick}/[001-201].JPG
—这是.....自定义文件名的下载? —对头,呵呵!
这样,自定义出来下载下来的文件名,就变成了这样:原来: ~zzh/001.JPG —-> 下载后: 001-zzh.JPG 原来: ~nick/001.JPG —-> 下载后: 001-nick.JPG
这样一来,就不怕文件重名啦,呵呵
9)继续讲下载
我们平时在windows平台上,flashget这样的工具可以帮我们分块并行下载,还可以断线续传。curl在这些方面也不输给谁,嘿嘿
比如我们下载screen1.JPG中,突然掉线了,我们就可以这样开始续传
$ curl -c -O http://cgi2.tky.3wb.ne.jp/~zzh/screen1.JPG
当然,你不要拿个flashget下载了一半的文件来糊弄我 别的下载软件的半截文件可不一定能用哦 ~
分块下载,我们使用这个option就可以了: -r
举例说明
比如我们有一个http://img.endv.cn/~zzh/zhao1.mp3 要下载(赵老师的电话朗诵 :D )我们就可以用这样的命令:
$ curl -r 0-10240 -o "zhao.part1" http:/img.endv.cn/~zzh/zhao1.mp3 &\ $ curl -r 10241-20480 -o "zhao.part1" http:/img.endv.cn/~zzh/zhao1.mp3 &\ $ curl -r 20481-40960 -o "zhao.part1" http:/img.endv.cn/~zzh/zhao1.mp3 &\ $ curl -r 40961- -o "zhao.part1" http:/img.endv.cn/~zzh/zhao1.mp3
这样就可以分块下载啦。不过你需要自己把这些破碎的文件合并起来如果你用UNIX或苹果,用 cat zhao.part* > zhao.mp3就可以如果用的是Windows,用copy /b 来解决吧,呵呵
上面讲的都是http协议的下载,其实ftp也一样可以用。用法嘛,
$ curl -u name:passwd ftp://ip:port/path/file
或者大家熟悉的
$ curl ftp://name:passwd@ip:port/path/file
10) 说完了下载,接下来自然该讲上传咯上传的option是 -T
比如我们向ftp传一个文件:
$ curl -T localfile -u name:passwd ftp://upload_site:port/path/
当然,向http服务器上传文件也可以比如
$ curl -T localfile http://img.endv.cn/~zzh/abc.cgi
注意,这时候,使用的协议是HTTP的PUT method
刚才说到PUT,嘿嘿,自然让老服想起来了其他几种methos还没讲呢! GET和POST都不能忘哦。
http提交一个表单,比较常用的是POST模式和GET模式
GET模式什么option都不用,只需要把变量写在url里面就可以了比如:
$ curl http://code.endv.cn/login.cgi?user=nickwolfe&password=12345
而POST模式的option则是 -d
比如,
$ curl -d "user=nickwolfe&password=12345" http://code.endv.cn/login.cgi
就相当于向这个站点发出一次登陆申请 ~
到底该用GET模式还是POST模式,要看对面服务器的程序设定。
一点需要注意的是,POST模式下的文件上的文件上传,比如
<form method="POST" enctype="multipar/form-data" action="http://img.endv.cn/~zzh/up_file.cgi"> <input type=file name=upload> <input type=submit name=nick value="go"> </form>
这样一个HTTP表单,我们要用curl进行模拟,就该是这样的语法:
$ curl -F upload=@localfile -F nick=go http://img.endv.cn/~zzh/up_file.cgi
罗罗嗦嗦讲了这么多,其实curl还有很多很多技巧和用法比如 https的时候使用本地证书,就可以这样
$ curl -E localcert.pem https://remote_server
再比如,你还可以用curl通过dict协议去查字典 ~
$ curl dict://dict.org/d:computer
推荐学习:《PHP视频教程》
The above is the detailed content of How to convert curl to php. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
