dongweicha6077 2018-05-04 10:25
浏览 88


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 '{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 '{identifier}'

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

I've tried using $curl = curl_init(""); 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= "";
    $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);
    print $result;
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
  • dongpiao0731 2018-05-04 10:34
    1. Locate your PHP.ini file (normally located at in the /bin folder of your Apache install for example.
    2. Open the PHP.ini in text editor
    3. Search or find the following : ;extension=php_curl.dll
    4. Uncomment this by removing the semi-colon ; before it
    5. Save and Close PHP.ini
    6. Restart your Web Server (Apache or Nginx for example)

    You can test if cURL is enabled using this sample code:

    // Test if cURL is enabled
    echo 'Curl: ', function_exists('curl_version') ? 'Enabled' . "\xA" : 'Disabled' . "\xA";
  • dongquexi1990 2018-05-04 11:25

    If you are only doing a simple GET to pull data then you can use file_get_contents() IF the fopen() http/https wrappers haven't been disabled. See

    Should you need to set an auth token in the header, define a cookie, etc. first then you'll need to set up/create a context first -

    Unfortunately, the file_get_contents() only works for GET requests. If you end up needing to do a POST, PUT, or DELETE then you'll need to get curl working for your PHP install, or perhaps use the system's curl and exec() or similar. Although if curl is disabled for security reasons I doubt you can use exec() and friends.

  • donglu8549 2018-05-04 12:03

    (there are a lot of similar question on StackOverflow, but the qulity of answers is not very good for all the ones I've looked at - hence not marking this as a duplicate)

    I can't tell you why it has been disabled on the installation you are using, but it is common practice on basic hosting packages to restrict the level of functionality and the applications ability to interact with the local infrastructure. I would like to think that conscientious package maintainers would also distribute PHP with as safe a default configuration as possible.

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

    There's 2 issues here - your understanding of how this should work and the error message that is preventing your code from running. If we worry about the latter then at least you will be in a position to address the former yourself.

    WARNING curl_init() has been disabled for security reasons

    This message indicates that either safe_mode is enabled or curl_init is included in the list of disabled functions in your PHP.ini.

    If you don't know where your php.ini file is - check the output of

    <?php phpinfo();

    Note that some ini settings can be overridden elsewhere - e.g. in Apache's httpd.config or .htaccess files - but not these ones.

    If you cannot access the ini file, or the setting are you can check these using:

       print ini_get('disable_functions') 
       . "<br>" . ini_get('disable_classes')
       . "<br>" . init_get('safe_mode');

    In the case of curl_init, we are really only interested in the first one of these. But as per above - if we are talking about host you don't have full control over, then it may be a constraint of your hosting package.

    IIRC, an entry in disable_functions will mask out a missing extension - so you might remove the entry from php.ini and start seeing undefined function errors. In that case you need to install the extension as described by others here (but the relevant filename is on Unix/Linux systems) remembering to restart httpd/php-fpm as appropriate.

    You can now call the function. That doesn't mean that your host will be able to:

    • resolve the hostname in the URL
    • establish an http/https connection

    But you'll see different errors.




  • ¥15 Qt 不小心删除了自带的类,该怎么办
  • ¥15 我需要在PC端 开两个抖店工作台客户端.(语言-java)
  • ¥15 有没有哪位厉害的人可以用C#可视化呀
  • ¥15 可以帮我看看代码哪里错了吗
  • ¥15 设计一个成绩管理系统
  • ¥15 PCL注册的选点等函数如何取消注册
  • ¥15 问一下各位,为什么我用蓝牙直接发送模拟输入的数据,接收端显示乱码呢,米思齐软件上usb串口显示正常的字符串呢?
  • ¥15 Python爬虫程序
  • ¥15 crypto 这种的应该怎么找flag?
  • ¥15 代码已写好,求帮我指出错误,有偿!