search
HomeBackend DevelopmentPHP TutorialUse NP-Gravatar to get the avatar on Gravatar

  1. //Creating instance:
  2. $gravatarService = new NP_Service_Gravatar_Profiles();
  3. //Changing response format to XML:
  4. $gravatarService->setResponseFormat(new NP_Service_Gravatar_Profiles_ResponseFormat_Xml());
  5. //Getting profile data.
  6. $profile = $gravatarService->getProfileInfo('foo@bar.com');
  7. //$profile is instance of NP_Gravatar_Profile so we can access some of its properties.
  8. echo 'ID: ' . $profile->id . '
    ';
  9. echo 'Username: ' . $profile->getPreferredUsername() . '

    ';
  10. echo 'Photos:
    ';
  11. foreach($profile->getPhotos() as $photo) {
  12. echo 'Use NP-Gravatar to get the avatar on Gravatar
    ';
  13. }
  14. //Changing response format to JSON:
  15. $gravatarService->setResponseFormat(new NP_Service_Gravatar_Profiles_ResponseFormat_Json());
  16. //Getting profile data but forcing raw Zend_Http_Response object to be returned,
  17. //by passing boolean true for the second argument of the getProfileInfo() method:
  18. $response = $gravatarService->getProfileInfo('foo@bar.com', true);
  19. if ($response instanceof Zend_Http_Response) { //true!
  20. //do something
  21. }
  22. //Changing response format to QR Code:
  23. $gravatarService->setResponseFormat(new NP_Service_Gravatar_Profiles_ResponseFormat_QRCode());
  24. //QR Code response can not be exported NP_Gravatar_Profile object, as that
  25. //response format type does not implement
  26. //NP_Service_Gravatar_Profiles_ResponseFormat_ParserInterface interface,
  27. //so raw Zend_Http_Response object will allways be returned when using
  28. //that response format:
  29. $response = $gravatarService->getProfileInfo('foo@bar.com');
  30. echo $response->getHeader('Content-type'); //Prints "image/png".
复制代码
  1. //Gravatar XML-RPC implementation requires API key for the
  2. //authentication proccess. It can be retrieved on the page
  3. //for editing profile, on wordpress.com.
  4. $apiKey = 'someAPIKey';
  5. $email = 'foo.bar@foobar.com'; //Email address associated with the $apiKey.
  6. //Creating instance:
  7. $gravatarXmlRpc = new NP_Service_Gravatar_XmlRpc($apiKey, $email);
  8. //Checking whether there's a gravatar account registered with supplied email addresses.
  9. $result = $gravatarXmlRpc->exists(array(
  10. 'posa.nikola@gmail.com', //That's me. :D
  11. 'foo@example.com'
  12. ));
  13. $values = array_values($result);
  14. echo (bool)$values[0]; //Prints "true", as I do have Gravatar account. :)
  15. echo (bool)$values[1]; //Prints "false", as that second email address probably doesn't exist.
  16. //Getting user images on the current account:
  17. $images = $gravatarXmlRpc->userImages();
  18. //$image is instance of NP_Service_Gravatar_XmlRpc_UserImage,
  19. //as we didn't pass $raw parameter as "true" when executing
  20. //userImages() method.
  21. $image = $images[0];
  22. $imageUrl = $image->getUrl(); //Instance of Zend_Uri_Http.
  23. echo $image->getRating(); //Prints some rating (G, PG, R or X).
  24. //Saves some image to be a user image for the current account.
  25. $this->_gravatarXmlRpc->saveData('path/to/someImage.jpg', NP_Service_Gravatar_XmlRpc::PG_RATED);
复制代码
  1. //Generating Gravatar URL.
  2. echo 'Use NP-Gravatar to get the avatar on Gravatar;
  3. //Generating Gravatar URL and specifying size and rating options.
  4. echo 'Use NP-Gravatar to get the avatar on Gravatar;
  5. //Full parameter names are supported, too.
  6. echo 'Use NP-Gravatar to get the avatar on Gravatar;
  7. //Generating Gravatar URL and specifying file-type extension.
  8. echo 'Use NP-Gravatar to get the avatar on Gravatar;
  9. //Above view helper call will produce this URL:
  10. //http://www.gravatar.com/avatar/f3ada405ce890b6f8204094deb12d8a8.jpg?s=200
复制代码


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Dependency Injection in PHP: Avoiding Common PitfallsDependency Injection in PHP: Avoiding Common PitfallsMay 16, 2025 am 12:17 AM

DependencyInjection(DI)inPHPenhancescodeflexibilityandtestabilitybydecouplingdependencycreationfromusage.ToimplementDIeffectively:1)UseDIcontainersjudiciouslytoavoidover-engineering.2)Avoidconstructoroverloadbylimitingdependenciestothreeorfour.3)Adhe

How to Speed Up Your PHP Website: Performance TuningHow to Speed Up Your PHP Website: Performance TuningMay 16, 2025 am 12:12 AM

ToimproveyourPHPwebsite'sperformance,usethesestrategies:1)ImplementopcodecachingwithOPcachetospeedupscriptinterpretation.2)Optimizedatabasequeriesbyselectingonlynecessaryfields.3)UsecachingsystemslikeRedisorMemcachedtoreducedatabaseload.4)Applyasynch

Sending Mass Emails with PHP: Is it Possible?Sending Mass Emails with PHP: Is it Possible?May 16, 2025 am 12:10 AM

Yes,itispossibletosendmassemailswithPHP.1)UselibrarieslikePHPMailerorSwiftMailerforefficientemailsending.2)Implementdelaysbetweenemailstoavoidspamflags.3)Personalizeemailsusingdynamiccontenttoimproveengagement.4)UsequeuesystemslikeRabbitMQorRedisforb

What is the purpose of Dependency Injection in PHP?What is the purpose of Dependency Injection in PHP?May 16, 2025 am 12:10 AM

DependencyInjection(DI)inPHPisadesignpatternthatachievesInversionofControl(IoC)byallowingdependenciestobeinjectedintoclasses,enhancingmodularity,testability,andflexibility.DIdecouplesclassesfromspecificimplementations,makingcodemoremanageableandadapt

How to send an email using PHP?How to send an email using PHP?May 16, 2025 am 12:03 AM

The best ways to send emails using PHP include: 1. Use PHP's mail() function to basic sending; 2. Use PHPMailer library to send more complex HTML mail; 3. Use transactional mail services such as SendGrid to improve reliability and analysis capabilities. With these methods, you can ensure that emails not only reach the inbox, but also attract recipients.

How to calculate the total number of elements in a PHP multidimensional array?How to calculate the total number of elements in a PHP multidimensional array?May 15, 2025 pm 09:00 PM

Calculating the total number of elements in a PHP multidimensional array can be done using recursive or iterative methods. 1. The recursive method counts by traversing the array and recursively processing nested arrays. 2. The iterative method uses the stack to simulate recursion to avoid depth problems. 3. The array_walk_recursive function can also be implemented, but it requires manual counting.

What are the characteristics of do-while loops in PHP?What are the characteristics of do-while loops in PHP?May 15, 2025 pm 08:57 PM

In PHP, the characteristic of a do-while loop is to ensure that the loop body is executed at least once, and then decide whether to continue the loop based on the conditions. 1) It executes the loop body before conditional checking, suitable for scenarios where operations need to be performed at least once, such as user input verification and menu systems. 2) However, the syntax of the do-while loop can cause confusion among newbies and may add unnecessary performance overhead.

How to hash strings in PHP?How to hash strings in PHP?May 15, 2025 pm 08:54 PM

Efficient hashing strings in PHP can use the following methods: 1. Use the md5 function for fast hashing, but is not suitable for password storage. 2. Use the sha256 function to improve security. 3. Use the password_hash function to process passwords to provide the highest security and convenience.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.