简体   繁体   中英

Fatal error: Class 'OAuth' not found in

I'm trying to connect to the LinkedIn API but everytime I try to access it I get the following error:

Fatal error: Class 'OAuth' not found in /home/vhosts/ * /test.php on line 8

I'm using a free server on 000WebHost and I've read that free servers sometimes don't support OAuth. I've tried it on another free server and I get the same error message, so my question is how can I check whether the server supports the use of OAuth?

Here is my code:

// Fill the keys and secrets you retrieved after registering your app
$oauth = new OAuth("abcd123456", "efgh987654");
$oauth->setToken("abcd1234-efgh987-9988", "9876abcd-123asdf-1122");

$params = array();
$headers = array();
$method = OAUTH_HTTP_METHOD_GET;

// Specify LinkedIn API endpoint to retrieve your own profile
$url = "http://api.linkedin.com/v1/people/~";

// By default, the LinkedIn API responses are in XML format. If you prefer JSON, simply       specify the format in your call
// $url = "http://api.linkedin.com/v1/people/~?format=json";

// Make call to LinkedIn to retrieve your own profile
$oauth->fetch($url, $params, $method, $headers);

echo $oauth->getLastResponse();

The keys are just replaced with the ones on the LikedIn Developers Getting Started Guide.

Thank you in advance for your help.

OAuth is a PECL extension it must be compiled into PHP or compiled as an extension.

Most servers will not have it by default since it really is not something everyone would likely use. You can ask your host to either install it or if you have the ability compile it on server if using CGI as I did. If you run phpinfo(); and look for the word OAuth it will show up if you have it, otherwise you don't.


Update: Use https://github.com/Lusitanian/PHPoAuthLib instead of a PECL.

I use hostgator for hosting and was having this problem so if your host also uses cpanel you should be able to do what I did.

Go 1 directory up from the live directory in File Manager where you can see "public_html, www, tmp". (Or click the home folder icon to the left) and in there you should find a php.ini file. edit the file adding extension=oauth.so to the very end and save it.

Checking phpinfo() after that you should find a section called "OAuth" and everything should work fine.

Try to look here . There is an example of how to use the linkedin api through OAuth php class not an extension.

For php 5.6... First the disclaimer: you need to migrate to the latest stable php 7 ASAP and not run php 5.6! But if that's just not possible quite yet, this might help Ubuntu 16.04 users. This assumes you have the ondrej/php ppa.

sudo apt-get update
sudo apt-get install libpcre3-dev
sudo apt-get install php-pear            # * see note below 
sudo apt-get install php5.6-dev          # for phpize
sudo pecl install oauth-1.2.3

# now add "extension=oauth.so" (sans quotes) to the 
# "Dynamic Extensions" area in /etc/php/5.6/apache2/php.ini

* Per this post https://askubuntu.com/a/756186/343695 "php-pear pull[s] just CLI PHP 7.0 (php7.0-cli) and that's harmless" That comment was made in 2016 and may no longer be true. I didn't see any problems...yet.

sudo apt-get update
sudo apt-get install php-oauth
sudo service apache2 restart

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM