猿问

如何使用卷曲来点击 API?

在这种情况下,我试图通过使用curl来点击API,我试图点击:https://jsonplaceholder.typicode.com/posts


  <?php


class 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


拉风的咖菲猫
浏览 94回答 1
1回答

红糖糍粑

请像这样使用:-<?phpclass process extends CI_Controller {&nbsp; &nbsp; function curlAPI(){&nbsp; &nbsp; &nbsp; &nbsp; $headers = 'Content-Type:application/json';&nbsp; &nbsp; &nbsp; &nbsp; $process = curl_init('https://jsonplaceholder.typicode.com/users'); //your API url&nbsp; &nbsp; &nbsp; &nbsp; curl_setopt($process, CURLOPT_HTTPHEADER, $headers);&nbsp; &nbsp; &nbsp; &nbsp; curl_setopt($process, CURLOPT_HEADER, 1);&nbsp; &nbsp; &nbsp; &nbsp; curl_setopt($process, CURLOPT_TIMEOUT, 20);&nbsp; &nbsp; &nbsp; &nbsp; curl_setopt($process, CURLOPT_POST, 1);&nbsp; &nbsp; &nbsp; &nbsp; curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);&nbsp; &nbsp; &nbsp; &nbsp; $return = curl_exec($process);&nbsp; &nbsp; &nbsp; &nbsp; curl_close($process);&nbsp; &nbsp; &nbsp; &nbsp; print_r($return);&nbsp; &nbsp; }}?>
随时随地看视频慕课网APP
我要回答