在这种情况下,我试图通过使用curl来点击API,我试图点击:https://jsonplaceholder.typicode.com/posts <?phpclass process extends CI_Controller { $headers = 'Content-Type:application/json'; $process = curl_init('https://jsonplaceholder.typicode.com/users'); //your API url curl_setopt($process, CURLOPT_HTTPHEADER, $headers); curl_setopt($process, CURLOPT_HEADER, 1); curl_setopt($process, CURLOPT_TIMEOUT, 20); curl_setopt($process, CURLOPT_POST, 1); curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE); $return = curl_exec($process); curl_close($process); //finally print your API response print_r($return);}但它显示了一些这样的错误遇到未捕获的异常 类型: 解析错误消息:语法错误、意外的“$headers”(T_VARIABLE)、期望函数 (T_FUNCTION) 或常量 (T_CONST)文件名: E:\XAMPP\硬件\Crudview\应用程序\控制器\进程.php线路代码: 6回溯:文件: E:\XAMPP\htdocs\crudview\索引.php 行: 315 函数: require_once
1 回答
红糖糍粑
TA贡献1815条经验 获得超6个赞
请像这样使用:-
<?php
class process extends CI_Controller {
function curlAPI(){
$headers = 'Content-Type:application/json';
$process = curl_init('https://jsonplaceholder.typicode.com/users'); //your API url
curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
curl_setopt($process, CURLOPT_HEADER, 1);
curl_setopt($process, CURLOPT_TIMEOUT, 20);
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($process);
curl_close($process);
print_r($return);
}
}
?>
- 1 回答
- 0 关注
- 93 浏览
添加回答
举报
0/150
提交
取消