Home>Article>Backend Development> How to convert curl to php

How to convert curl to php

藏色散人
藏色散人 Original
2021-11-04 09:14:46 1925browse

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/

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

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

  Welcome .
You are years old.

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 d8a1c03fa9e6aaa0a6f4d1e085fe1ce1 下载后: 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模式下的文件上的文件上传,比如

这样一个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!

Statement:
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