Reputation: 23
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)
Upvotes: 2
Views: 4162
Reputation: 48357
(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:
But you'll see different errors.
Upvotes: 0
Reputation: 2215
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.
Upvotes: 0
Reputation: 4894
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;
Upvotes: 0
Reputation: 1058
;extension=php_curl.dll
;
before itYou 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";
?>
Upvotes: 1