PHP用CURL发送Content-type为application/json的HTTP请求

如何用php模拟json请求

只需设置header头

```
$url ="http://www.test.com/articles/_search?size=10&from=$from";
$json_data = json_encode ( $params );
$json_data = "{'name':'test'}";
$result = curl ( $url, $json_data, "GET" );
```

```
function curl($url, $postDate = "", $method = "PUT") {
//echo "url=>".$url."
";
$ci = curl_init ();
$headers = array(
"Content-type: application/json;charset='utf-8'",
"Accept: application/json",
"Cache-Control: no-cache",
"Pragma: no-cache",
);
curl_setopt ( $ci, CURLOPT_URL, $url );
curl_setopt ( $ci, CURLOPT_PORT, 10026 );
curl_setopt ( $ci, CURLOPT_TIMEOUT, 200 );
curl_setopt ( $ci, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ci, CURLOPT_FORBID_REUSE, 0 );
curl_setopt ( $ci, CURLOPT_CUSTOMREQUEST, $method );
curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
if ($postDate) {
curl_setopt ( $ci, CURLOPT_POSTFIELDS, $postDate );
}
$response = curl_exec ( $ci );
return $response;
}
```

    A+
发布日期:2021年05月30日  所属分类:未分类

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: