dongweicha6077 2018-05-04 10:25
浏览 88
已采纳

来自API的curl请求与php

I need to get data from an API using curl in PHP, but I have no experience with this?

I've been using PHP for a while now (beginer/intermediate level), but I don't know how to use the curl command or how to make a PHP version of this.

All I know of the API is that to get a list of all the books I kan use:

curl --request GET \
--url 'http://example.com/{identifier}'

using "main" as the {identifier}

This is supposed to give me a list of alle the books, and then I can use

curl --request GET \
--url 'http://example.com/feed/{identifier}'

to get the (meta) data of the books (Title, Author, Description, Year, Cover)

I've tried using $curl = curl_init("http://example.com/feed/main"); and then using curl_setopt_array($curl, array() to get the data, but i get an "WARNING curl_init() has been disabled for security reasons" error.

I'm totally lost and starting from scratch on these API/curl calls...

Hope someone can explain it to me?

(using Xampp on Windows)

  • 写回答

4条回答 默认 最新

  • dongzhizhai4070 2018-05-04 10:38
    关注

    First enable curl for your xamp. To do so follow the steps
    1. Go to C:\Program Files\xampp\php\php.ini
    2. Un-comment the following line on your php.ini file by removing the semicolon.
    ;extension=php_curl.dll to extension=php_curl.dll
    3. Restart your apache server.

    Then user thr curl like

    $url= "http://example.com/feed/main";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_ENCODING, ""); // this will handle gzip content
    $result = curl_exec($ch);
    curl_close($ch);
    print $result;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵