How to convert curl to php

藏色散人
Release: 2023-03-13 17:32:01
Original
1922 people have browsed it

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.

How to convert curl to php

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/
Copy after login

Return

{"status": 200, "datapoint": null}
Copy after login

Set status

curl -H "Authorization: token 6bcb3cdb69b07370f5ad73e7a856409802fdd735" -d "{\"datapoint\":{\"x\":1}}" http://user.endv.cn/v1/datastreams/plug-status/datapoint/?deliver_to_device=true
Copy after login

Return

{"status": 404, "nonce": 333984364, "message": "remote device is disconnect"}
Copy after login

curl to php and send

Get status:

         
Copy after login

Set status:

         
Copy after login

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);
Copy after login

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);
Copy after login

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"}
Copy after login

curl post sending and receiving

 "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); ?>
Copy after login

  Welcome .
You are years old.
Copy after login

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);
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

Here we teach you a new option: -O capital O, use it like this:

$ curl -O http://img.endv.cn/~zzh/screen1.JPG
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

—这是.....自定义文件名的下载? —对头,呵呵!

这样,自定义出来下载下来的文件名,就变成了这样:原来: ~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
Copy after login

当然,你不要拿个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
Copy after login

这样就可以分块下载啦。不过你需要自己把这些破碎的文件合并起来如果你用UNIX或苹果,用 cat zhao.part* > zhao.mp3就可以如果用的是Windows,用copy /b 来解决吧,呵呵

上面讲的都是http协议的下载,其实ftp也一样可以用。用法嘛,

$ curl -u name:passwd ftp://ip:port/path/file
Copy after login

或者大家熟悉的

$ curl ftp://name:passwd@ip:port/path/file
Copy after login

10) 说完了下载,接下来自然该讲上传咯上传的option是 -T

比如我们向ftp传一个文件:

$ curl -T localfile -u name:passwd ftp://upload_site:port/path/
Copy after login

当然,向http服务器上传文件也可以比如

$ curl -T localfile http://img.endv.cn/~zzh/abc.cgi
Copy after login

注意,这时候,使用的协议是HTTP的PUT method

刚才说到PUT,嘿嘿,自然让老服想起来了其他几种methos还没讲呢! GET和POST都不能忘哦。

http提交一个表单,比较常用的是POST模式和GET模式

GET模式什么option都不用,只需要把变量写在url里面就可以了比如:

$ curl http://code.endv.cn/login.cgi?user=nickwolfe&password=12345
Copy after login

而POST模式的option则是 -d

比如,

$ curl -d "user=nickwolfe&password=12345" http://code.endv.cn/login.cgi
Copy after login

就相当于向这个站点发出一次登陆申请 ~

到底该用GET模式还是POST模式,要看对面服务器的程序设定。

一点需要注意的是,POST模式下的文件上的文件上传,比如

Copy after login

这样一个HTTP表单,我们要用curl进行模拟,就该是这样的语法:

$ curl -F upload=@localfile -F nick=go http://img.endv.cn/~zzh/up_file.cgi
Copy after login

罗罗嗦嗦讲了这么多,其实curl还有很多很多技巧和用法比如 https的时候使用本地证书,就可以这样

$ curl -E localcert.pem https://remote_server
Copy after login

再比如,你还可以用curl通过dict协议去查字典 ~

$ curl dict://dict.org/d:computer
Copy after login

推荐学习:《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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!