一段获取后端API数据内容的PHP代码

这段代码用于获取后端API数据,比如对接一些新闻网站提供的API数据接口。

<?php
$curl = curl_init();
curl_setopt_array($curl, [
	CURLOPT_URL => "your api address",
	CURLOPT_RETURNTRANSFER => true,
	CURLOPT_FOLLOWLOCATION => true,
	CURLOPT_ENCODING => "",
	CURLOPT_MAXREDIRS => 10,
	CURLOPT_TIMEOUT => 30,
	CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
	CURLOPT_CUSTOMREQUEST => "GET",
	CURLOPT_HTTPHEADER => [
		"data-name-one: your-data-one",
                "data-name-two: your-data-two",
	],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
	echo "cURL Error #:" . $err;
} else {
	echo $response;
}
?>

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注