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

来自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)

图片转代码服务由CSDN问答提供 功能建议

我需要在PHP中使用curl从API获取数据,但我对此没有经验?

我一直在使用PHP一段时间(初级/中级),但我不知道如何使用curl命令或如何制作PHP版本。

我所知道的API是获取我可以使用的所有书籍的列表:

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

使用“main”作为 {identifier} < / p>

这应该给我一个书籍的列表,然后我可以使用

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

获取图书的(元)数据(标题,作者, 说明,年份,封面)

我尝试使用 $ curl = curl_init(“http://example.com/feed/main”); 和 然后使用 curl_setopt_array($ curl,array()来获取数据,但我得到一个“WARNING curl_init ()出于安全原因已被禁用“错误。

我完全迷失了,并从头开始对这些API / curl调用...

希望有人可以向我解释一下吗?

(在Windows上使用Xampp)

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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;
    
    点赞 评论
  • 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:

    <?php
    // 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 http://php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen

    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 - http://php.net/manual/en/function.stream-context-create.php

    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:

     <?php
       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 php_curl.so 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.

    点赞 评论

相关推荐 更多相似问题