├── .gitignore ├── LICENSE ├── README.md ├── alchemyapi.php ├── composer.json ├── example.php ├── grumpy-cat-meme-hmmm.jpg └── tests.php /.gitignore: -------------------------------------------------------------------------------- 1 | api_key.txt 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # alchemyapi_php # 2 | 3 | A software development kit (sdk) for AlchemyAPI using PHP - **_Please note that this legacy AlchemyAPI SDK is no longer supported by IBM. Please use the Watson SDKs https://github.com/watson-developer-cloud?utf8=✓&query=sdk_** 4 | 5 | 6 | 7 | ## AlchemyAPI ## 8 | 9 | AlchemyAPI offers artificial intelligence as a service. We teach computers to learn how to read and see, and apply our technology to text analysis and image recognition through a cloud-based API. Our customers use AlchemyAPI to transform their unstructured content such as blog posts, news articles, social media posts and images into much more useful structured data. 10 | 11 | AlchemyAPI is a tech startup located in downtown Denver, Colorado. As the world's most popular text analysis service, AlchemyAPI serves over 3.5 billion monthly API requests to over 35,000 developers. To enable our services, we use artificial intelligence, machine learning, neural networks, natural language processing and massive-scale web crawling. Our technology powers use cases in a variety of industry verticals, including social media monitoring, business intelligence, content recommendations, financial trading and targeted advertising. 12 | 13 | More information at: http://www.alchemyapi.com 14 | 15 | 16 | 17 | ## API Key ## 18 | 19 | To use AlchemyAPI, you'll need to obtain an API key and attach that key to all requests. If you do not already have a key, please visit: http://www.alchemyapi.com/api/register.html 20 | 21 | 22 | 23 | ## Demo ## 24 | 25 | To use our interactive web demo, which includes access to all of AlchemyAPI's text analysis functions, please visit: http://www.alchemyapi.com/products/demo. You can use the demo to get a better understanding of how the text analysis functions work, and run some of your sample data through it to see what kind of results you can expect. The demo calls the exact same API functions as this SDK. 26 | 27 | 28 | 29 | ## Getting Started with the PHP SDK ## 30 | 31 | To get started and run the example, simply: 32 | 33 | git clone https://github.com/AlchemyAPI/alchemyapi_php.git 34 | cd alchemyapi_php 35 | php alchemyapi.php YOUR_API_KEY 36 | php example.php 37 | 38 | 39 | Just replace YOUR_API_KEY with your 40 character API key from AlchemyAPI, and you should be good to go. 40 | 41 | 42 | 43 | ## Using the PHP SDK ## 44 | 45 | This SDK makes accessing all of AlchemyAPI's text analysis functions with PHP easy. The following is a list of the available functions and how to use them within your application. For working code examples of these, please checkout the example code and run the example as described in the "Getting Started with the PHP SDK" section above. 46 | 47 | 48 | 49 | ### Creating the AlchemyAPI object ### 50 | 51 | To use each function, you must first create an AlchemyAPI object. The easiest way to do this is: 52 | 53 | 57 | 58 | You can now use this object to access AlchemyAPI's text analysis functions. 59 | 60 | ### Image Keywords ### 61 | 62 | The Image Keywords API tags an image identified by a URL or image data posted in the body of an html request. For more high-level information on AlchemyAPI's Image keywords API, please visit: http://www.alchemyapi.com/products/features/image-tagging. For more technical information, please refer to the docs: http://www.alchemyapi.com/api/image-tagging. 63 | 64 | To extract entities, use: 65 | 66 | image_keywords(FLAVOR, DATA, OPTIONS); 68 | ?> 69 | 70 | Where FLAVOR can be 'url' or 'image' DATA is your url or uri-argument encoded image data, and OPTIONS is an array containing the optional parameters to modify the behavior of the call. 71 | 72 | 73 | **Options** 74 | 75 | The following options are available for this call. To use, include your desired options into an array and pass it as the OPTIONS parameter in the call. 76 | 77 | - imagePostMode -> (only applicable to image flavor) 78 | - not-raw : pass an unencoded image file with "image=URI_ENCODED_DATA" 79 | - raw : pass an unencoded image file using POST 80 | - extractMode -> 81 | - always-infer : (more CPU intensive, more accurate) 82 | - trust-metadata : (less CPU intensive, less accurate) (default) 83 | - only-metadata : (even less CPU intensive, less accurate) 84 | 85 | **Parsing** 86 | 87 | To parse the results, simply step through the response structure that is detailed in the docs. For example, here's how to print the keyword and score of each tagged image: 88 | 89 | image_keywords('url','http://www.lolcats.com/images/u/08/50/lolcatsdotcomur5dhkw464f8hb16.jpg', null); 91 | foreach ($response['imageKeywords'] as $imagekeyword) { 92 | echo 'keyword: ', $imagekeyword['text'], PHP_EOL; 93 | echo 'score: ', $imagekeyword['score'], PHP_EOL; 94 | } 95 | ?> 96 | 97 | This should print out: 98 | 99 | keyword: cat 100 | score: 0.999697 101 | keyword: kitten 102 | score: 0.942676 103 | 104 | 105 | 106 | 107 | ### Entities ### 108 | 109 | The entity extraction API identifies the proper nouns in your text, HTML or URL content. These are the named people, companies, organizations and locations. For more high-level information on AlchemyAPI's entity extraction API, please visit: http://www.alchemyapi.com/products/features/entity-extraction. For more technical information, please refer to the docs: http://www.alchemyapi.com/api/entity-extraction. 110 | 111 | To extract entities, use: 112 | 113 | entities(FLAVOR, DATA, OPTIONS); 115 | ?> 116 | 117 | Where FLAVOR can be 'text','html', or 'url', DATA is your text, html or url content, and OPTIONS is an array containing the optional parameters to modify the behavior of the call. 118 | 119 | 120 | **Options** 121 | 122 | The following options are available for this call. To use, include your desired options into an array and pass it as the OPTIONS parameter in the call. 123 | 124 | - disambiguate -> disambiguate entities (i.e. Apple the company vs. apple the fruit). 0: disabled, 1: enabled (default) 125 | - linkedData -> include linked data on disambiguated entities. 0: disabled, 1: enabled (default) 126 | - coreference -> resolve coreferences (i.e. the pronouns that correspond to named entities). 0: disabled, 1: enabled (default) 127 | - quotations -> extract quotations by entities. 0: disabled (default), 1: enabled. 128 | - sentiment -> analyze sentiment for each entity. 0: disabled (default), 1: enabled. Requires 1 additional API transction if enabled. 129 | - showSourceText -> 0: disabled (default), 1: enabled 130 | - maxRetrieve -> the maximum number of entities to retrieve (default: 50) 131 | 132 | 133 | **Parsing** 134 | 135 | To parse the results, simply step through the response structure that is detailed in the docs. For example, here's how to print the text and type of each extracted entity: 136 | 137 | entities('text','I like Ford cars better than icecream.', null); 139 | foreach ($response['entities'] as $entity) { 140 | echo 'entity: ', $entity['text'], PHP_EOL; 141 | echo 'type: ', $entity['type'], PHP_EOL; 142 | } 143 | ?> 144 | 145 | This should print out: 146 | 147 | entity: Ford 148 | type: Company 149 | 150 | 151 | 152 | ### Keywords ### 153 | 154 | The keyword extraction API identifies the important terms in your text, HTML or URL content. These are known by several names, but keywords, tags and terms are the most common. For more high-level information on AlchemyAPI's keyword extraction API, please visit: http://www.alchemyapi.com/products/features/keyword-extraction. For more technical information, please refer to the docs: http://www.alchemyapi.com/api/keyword-extraction. 155 | 156 | To extract keywords, use: 157 | 158 | keywords(FLAVOR, DATA, OPTIONS); 160 | ?> 161 | 162 | Where FLAVOR can be 'text','html', or 'url', DATA is your text, html or url content, and OPTIONS is an array containing the optional parameters to modify the behavior of the call. 163 | 164 | 165 | **Options** 166 | 167 | The following options are available for this call. To use, include your desired options into an array and pass it as the OPTIONS parameter in the call. 168 | 169 | - keywordExtractMode -> normal (default), strict 170 | - sentiment -> analyze sentiment for each keyword. 0: disabled (default), 1: enabled. Requires 1 additional API transaction if enabled. 171 | - showSourceText -> 0: disabled (default), 1: enabled. 172 | - maxRetrieve -> the max number of keywords returned (default: 50) 173 | 174 | 175 | **Parsing** 176 | 177 | To parse the results, simply step through the response structure that is detailed in the docs. For example, here's how to print the text and relevance of each extracted keyword: 178 | 179 | keywords('text', 'Bunnies are nice but sometimes robots are evil', array('sentiment'=>1)); 181 | foreach ($response['keywords'] as $keyword) { 182 | echo 'keyword: ', $keyword['text'], PHP_EOL; 183 | echo 'relevance: ', $keyword['relevance'], PHP_EOL; 184 | echo 'sentiment: ', $keyword['sentiment']['type'], PHP_EOL; 185 | } 186 | ?> 187 | 188 | This should print out: 189 | 190 | keyword: Bunnies 191 | relevance: 0.981751 192 | sentiment: positive 193 | keyword: robots 194 | relevance: 0.832378 195 | sentiment: negative 196 | 197 | 198 | 199 | ### Concepts ### 200 | 201 | The concept tagging API identifies the high-level themes of your text, HTML or URL content. Unlike keywords, the concepts do not have to be specifically mentioned in the content. For more high-level information on AlchemyAPI's concept tagging API, please visit: http://www.alchemyapi.com/products/features/concept-tagging. For more technical information, please refer to the docs: http://www.alchemyapi.com/api/concept-tagging. 202 | 203 | To tag concepts, use: 204 | 205 | concepts(FLAVOR, DATA, OPTIONS); 207 | ?> 208 | 209 | Where FLAVOR can be 'text','html', or 'url', DATA is your text, html or url content, and OPTIONS is an array containing the optional parameters to modify the behavior of the call. 210 | 211 | 212 | **Options** 213 | 214 | The following options are available for this call. To use, include your desired options into an array and pass it as the OPTIONS parameter in the call. 215 | 216 | - maxRetrieve -> the maximum number of concepts to retrieve (default: 8) 217 | - linkedData -> include linked data, 0: disabled, 1: enabled (default) 218 | - showSourceText -> 0:disabled (default), 1: enabled 219 | 220 | 221 | **Parsing** 222 | 223 | To parse the results, simply step through the response structure that is detailed in the docs. For example, here's how to print the text and relevance of each concept: 224 | 225 | concepts('text','Today for lunch I ate two apples and one strawberry.', null); 227 | foreach ($response['concepts'] as $concept) { 228 | echo 'concept: ', $concept['text'], PHP_EOL; 229 | echo 'relevance: ', $concept['relevance'], PHP_EOL; 230 | } 231 | ?> 232 | 233 | This should print out: 234 | 235 | concept: Fruit 236 | relevance: 0.865419 237 | 238 | 239 | 240 | ### Sentiment ### 241 | 242 | The sentiment analysis API calculates the document-level sentiment of your text, HTML or URL content. Sentiment determines whether the content is positive or negative, and can be calculated for the entire document, a user-specified target, or at the keyword, entity or relational level. This API call is only for document sentiment. To get keyword, entity or relational sentiment, set the OPTION parameter {'sentiment':1} in the respective call. Targeted sentiment is handled by a seperate call as well. For more high-level information on AlchemyAPI's sentiment analysis API, please visit: http://www.alchemyapi.com/products/features/sentiment-analysis. For more technical information, please refer to the docs: http://www.alchemyapi.com/api/sentiment-analysis. 243 | 244 | To calculate document-level sentiment, use: 245 | 246 | sentiment(FLAVOR, DATA, OPTIONS); 248 | ?> 249 | 250 | Where FLAVOR can be 'text','html', or 'url', DATA is your text, html or url content, and OPTIONS is an array containing the optional parameters to modify the behavior of the call. 251 | 252 | 253 | **Options** 254 | 255 | The following options are available for this call. To use, include your desired options into an array and pass it as the OPTIONS parameter in the call. 256 | 257 | - showSourceText -> 0: disabled (default), 1: enabled 258 | 259 | 260 | **Parsing** 261 | 262 | To parse the results, simply step through the response structure that is detailed in the docs. For example, here's how to print the type and score of the document sentiment: 263 | 264 | sentiment('text','I really like chocolate cake.', null); 266 | echo 'sentiment: ', $response['docSentiment']['type'], PHP_EOL; 267 | echo 'score: ', $response['docSentiment']['score'], PHP_EOL; 268 | ?> 269 | 270 | This should print out: 271 | 272 | sentiment: positive 273 | score: 0.631595 274 | 275 | 276 | NOTE: The sentiment type can be either positive, negative or neutral. The score is only returned for positive and negative sentiment, and is not included in the response structure for neutral sentiment. 277 | 278 | 279 | 280 | ### Targeted Sentiment ### 281 | 282 | The targeted sentiment analysis API calculates the document-level sentiment of your text, HTML or URL content in relation to a specified target. The targets are typically brand names, product names, specific features and similar. Please see the sentiment section above for more info about sentiment analysis. 283 | 284 | To calculate targeted sentiment for an entire document, use: 285 | 286 | sentiment_targeted(FLAVOR, DATA, TARGET, OPTIONS); 288 | ?> 289 | 290 | Where FLAVOR can be 'text','html', or 'url', DATA is your text, html or url content, TARGET is the user-specified target, and OPTIONS is an array containing the optional parameters to modify the behavior of the call. 291 | 292 | **Options** 293 | 294 | The following options are available for this call. To use, include your desired options into an array and pass it as the OPTIONS parameter in the call. 295 | 296 | - showSourceText -> 0: disabled, 1: enabled 297 | 298 | 299 | **Parsing** 300 | 301 | To parse the results, simply step through the response structure that is detailed in the docs. For example, here's how to print the type and score of the targeted sentiment: 302 | 303 | sentiment_targeted('text','My new phones screen is awesome, but the battery life is really bad.','battery life', null); 305 | echo 'sentiment: ', $response['docSentiment']['type'], PHP_EOL; 306 | echo 'relevance: ', $response['docSentiment']['score'], PHP_EOL; 307 | ?> 308 | 309 | This should print out: 310 | 311 | sentiment: negative 312 | relevance: -0.128125 313 | 314 | 315 | NOTE: The sentiment type can be either positive, negative or neutral. The score is only returned for positive and negative sentiment, and is not included in the response structure for neutral sentiment. 316 | 317 | 318 | 319 | ### Text ### 320 | 321 | The text extraction API pulls out the important content from your HTML or URL content and leaves the unimportant content like navigation and ads behind. For more high-level information on AlchemyAPI's text extraction API, please visit: http://www.alchemyapi.com/products/features/text-extraction. For more technical information, please refer to the docs: http://www.alchemyapi.com/api/text-extraction. 322 | 323 | To extract the text, use: 324 | 325 | text(FLAVOR, DATA, OPTIONS); 327 | ?> 328 | 329 | Where FLAVOR can be 'html' or 'url', DATA is your html or url content and OPTIONS is an array containing the optional parameters to modify the behavior of the call. 330 | 331 | 332 | **Options** 333 | 334 | The following options are available for this call. To use, include your desired options into an array and pass it as the OPTIONS parameter in the call. 335 | 336 | - useMetadata -> utilize meta description data, 0: disabled, 1: enabled (default) 337 | - extractLinks -> include links, 0: disabled (default), 1: enabled. 338 | 339 | 340 | **Parsing** 341 | 342 | To parse the results, simply step through the response structure that is detailed in the docs. For example, here's how to print the extracted text: 343 | 344 | text('url','http://www.alchemyapi.com/products/features/text-extraction/',null); 346 | echo 'text: ', $response['text'], PHP_EOL; 347 | ?> 348 | 349 | This should print out: 350 | 351 | text: ... the text from the text extraction webpage ... 352 | 353 | 354 | The above extracts the "cleaned" text. If you would like to get the raw text instead, use: 355 | 356 | text_raw(FLAVOR, DATA, OPTIONS); 358 | ?> 359 | 360 | The parameters and parsing are the same as for the cleaned text call. 361 | 362 | 363 | 364 | ### Author ### 365 | 366 | The author extraction API identifies the author of your HTML or URL content. For more high-level information on AlchemyAPI's author extraction API, please visit: http://www.alchemyapi.com/products/features/author-extraction. For more technical information, please refer to the docs: http://www.alchemyapi.com/api/author-extraction. 367 | 368 | To extract the author, use: 369 | 370 | author(FLAVOR, DATA, OPTIONS); 372 | ?> 373 | 374 | Where FLAVOR can be 'html' or 'url', DATA is your html or url content, and OPTIONS is an array containing the optional parameters to modify the behavior of the call. 375 | 376 | 377 | **Options** 378 | 379 | There are no available options for this call. 380 | 381 | 382 | **Parsing** 383 | 384 | To parse the results, simply step through the response structure that is detailed in the docs. For example, here's how to print the author: 385 | 386 | author('url','http://www.grantland.com/story/_/id/9566213/bill-barnwell-puts-2013-detroit-vikings-numbers-test',null); 388 | 389 | echo 'author: ', $response['author'], PHP_EOL; 390 | ?> 391 | 392 | This should print out: 393 | 394 | author: Bill Barnwell 395 | 396 | 397 | 398 | ### Language ### 399 | 400 | The language detection API identifies the language of your text, HTML or URL content. It currently supports detection of over 97 languages. For more high-level information on AlchemyAPI's language detection API, please visit: http://www.alchemyapi.com/products/features/language-detection. For more technical information, please refer to the docs: http://www.alchemyapi.com/api/language-detection. 401 | 402 | To detect the language, use: 403 | 404 | language(FLAVOR, DATA, OPTIONS); 406 | ?> 407 | 408 | Where FLAVOR can be 'text', 'html' or 'url', DATA is your text, html or url content, and OPTIONS is an array containing the optional parameters to modify the behavior of the call. 409 | 410 | 411 | **Options** 412 | 413 | There are no available options for this call. 414 | 415 | 416 | **Parsing** 417 | 418 | To parse the results, simply step through the response structure that is detailed in the docs. For example, here's how to print the language and ISO-639-1 code: 419 | 420 | language('text','donde estas el bano?', null); 422 | echo 'language: ', $response['language'], PHP_EOL; 423 | echo 'iso-639-1: ', $response['iso-639-1'], PHP_EOL; 424 | ?> 425 | 426 | This should print out: 427 | 428 | language: spanish 429 | iso-639-1: es 430 | 431 | 432 | 433 | ### Title ### 434 | 435 | The title extraction API identifies the title of your HTML or URL content. For more high-level information on AlchemyAPI's title extraction API, please visit: http://www.alchemyapi.com/products/features/text-extraction. For more technical information, please refer to the docs: http://www.alchemyapi.com/api/text-extraction. 436 | 437 | To extract the title, use: 438 | 439 | title(FLAVOR, DATA, OPTIONS); 441 | ?> 442 | 443 | Where FLAVOR can be 'html' or 'url', DATA is your html or url content, and OPTIONS is an array containing the optional parameters to modify the behavior of the call. 444 | 445 | 446 | **Options** 447 | 448 | The following options are available for this call. To use, include your desired options into an array and pass it as the OPTIONS parameter in the call. 449 | 450 | - useMetadata -> utilize title info embedded in meta data, 0: disabled, 1: enabled (default) 451 | 452 | 453 | **Parsing** 454 | 455 | To parse the results, simply step through the response structure that is detailed in the docs. For example, here's how to print the title: 456 | 457 | title('url','http://www.bloomberg.com/news/2013-08-13/florida-to-sue-georgia-in-u-s-supreme-court-over-water.html', null); 459 | echo 'title: ', $response['title'], PHP_EOL; 460 | ?> 461 | 462 | This should print out: 463 | 464 | title: Florida to Sue Georgia in U.S. Supreme Court Over Water - Bloomberg 465 | 466 | 467 | 468 | ### Relations ### 469 | 470 | The relation extraction API identifies the Subject, Action, Object relations in your text, HTML or URL content. In addition to sentiment analysis, there are lots of options for this call, so be sure to check out the docs. For more high-level information on AlchemyAPI's relation extraction API, please visit: http://www.alchemyapi.com/products/features/relation-extraction. For more technical information, please refer to the docs: http://www.alchemyapi.com/api/relation-extraction. 471 | 472 | To extract relations, use: 473 | 474 | relations(FLAVOR, DATA, OPTIONS); 476 | ?> 477 | 478 | Where FLAVOR can be 'text','html', or 'url', DATA is your text, html or url content, and OPTIONS is an array containing the optional parameters to modify the behavior of the call. 479 | 480 | 481 | **Options** 482 | 483 | The following options are available for this call. To use, include your desired options into an array and pass it as the OPTIONS parameter in the call. 484 | 485 | - sentiment -> 0: disabled (default), 1: enabled. Requires one additional API transaction if enabled. 486 | - keywords -> extract keywords from the subject and object. 0: disabled (default), 1: enabled. Requires one additional API transaction if enabled. 487 | - entities -> extract entities from the subject and object. 0: disabled (default), 1: enabled. Requires one additional API transaction if enabled. 488 | - requireEntities -> only extract relations that have entities. 0: disabled (default), 1: enabled. 489 | - sentimentExcludeEntities -> exclude full entity name in sentiment analysis. 0: disabled, 1: enabled (default) 490 | - disambiguate -> disambiguate entities (i.e. Apple the company vs. apple the fruit). 0: disabled, 1: enabled (default) 491 | - linkedData -> include linked data with disambiguated entities. 0: disabled, 1: enabled (default). 492 | - coreference -> resolve entity coreferences. 0: disabled, 1: enabled (default) 493 | - showSourceText -> 0: disabled (default), 1: enabled. 494 | - maxRetrieve -> the maximum number of relations to extract (default: 50, max: 100) 495 | 496 | 497 | **Parsing** 498 | 499 | To parse the results, simply step through the response structure that is detailed in the docs. For example, here's how to print the subject, action and object of each extracted relation: 500 | 501 | relations('text','Bob broke my heart.',null); 503 | foreach ($response['relations'] as $relation) { 504 | if (array_key_exists('subject', $relation)) { 505 | echo 'Subject: ', $relation['subject']['text'], PHP_EOL; 506 | } 507 | 508 | if (array_key_exists('action', $relation)) { 509 | echo 'Action: ', $relation['action']['text'], PHP_EOL; 510 | } 511 | 512 | if (array_key_exists('object', $relation)) { 513 | echo 'Object: ', $relation['object']['text'], PHP_EOL; 514 | } 515 | echo PHP_EOL; 516 | } 517 | ?> 518 | 519 | This should print out: 520 | 521 | Subject: Bob 522 | Action: broke 523 | Object: my heart 524 | 525 | 526 | 527 | ### Category ### 528 | 529 | The text categorization API identifies the high-level category of your text, HTML or URL content. The categories include values such as Arts & Entertainment, Business and Sports. For more high-level information on AlchemyAPI's text categorization API, please visit: http://www.alchemyapi.com/products/features/text-categorization. For more technical information, please refer to the docs: http://www.alchemyapi.com/api/text-categorization. 530 | 531 | To detect the category, use: 532 | 533 | category(FLAVOR, DATA, OPTIONS); 535 | ?> 536 | 537 | Where FLAVOR can be 'text', 'html' or 'url', DATA is your text, html or url content, and OPTIONS is an array containing the optional parameters to modify the behavior of the call. 538 | 539 | 540 | **Options** 541 | 542 | The following options are available for this call. To use, include your desired options into an array and pass it as the OPTIONS parameter in the call. 543 | 544 | - showSourceText -> 0: disabled (default), 1: enabled 545 | 546 | 547 | **Parsing** 548 | 549 | To parse the results, simply step through the response structure that is detailed in the docs. For example, here's how to print the category: 550 | 551 | category('url','http://www.washingtonpost.com/blogs/capital-weather-gang/wp/2013/08/14/d-c-area-forecast-ultra-nice-weather-dominates-next-few-days/', null); 553 | echo 'category: ', $response['category'], PHP_EOL; 554 | ?> 555 | 556 | This should print out: 557 | 558 | category: weather 559 | 560 | 561 | 562 | ### Feeds ### 563 | 564 | The feed detection API extracts the RSS/ATOM feeds embedded in your HTML or URL content. For more high-level information on AlchemyAPI's feed detection API, please visit: http://www.alchemyapi.com/products/features/feed-detection. For more technical information, please refer to the docs: http://www.alchemyapi.com/api/feed-detection. 565 | 566 | To detect the feeds, use: 567 | 568 | feeds(FLAVOR, DATA, OPTIONS); 570 | ?> 571 | 572 | Where FLAVOR can be 'html' or 'url', DATA is your html or url content, and OPTIONS is an array containing the optional parameters to modify the behavior of the call. 573 | 574 | 575 | **Options** 576 | 577 | There are no available options for this call. 578 | 579 | 580 | **Parsing** 581 | 582 | To parse the results, simply step through the response structure that is detailed in the docs. For example, here's how to print the feeds: 583 | 584 | feeds('url','http://cnn.com', null); 586 | foreach ($response['feeds'] as $feed) { 587 | echo 'feed: ', $feed['feed'], PHP_EOL; 588 | } 589 | ?> 590 | 591 | This should print out: 592 | 593 | feed: http://rss.cnn.com/rss/cnn_topstories.rss 594 | feed: http://rss.cnn.com/rss/cnn_latest.rss 595 | 596 | 597 | 598 | ### Microformats ### 599 | 600 | The microformat parsing API extracts the microformats embedded in your HTML or URL content. For more high-level information on AlchemyAPI's microformats parsing API, please visit: http://www.alchemyapi.com/products/features/microformats-parsing. For more technical information, please refer to the docs: http://www.alchemyapi.com/api/microformats-parsing. 601 | 602 | To detect the microformats, use: 603 | 604 | microformats(FLAVOR, DATA, OPTIONS); 606 | ?> 607 | 608 | Where FLAVOR can be 'html' or 'url', DATA is your html or url content, and OPTIONS is an array containing the optional parameters to modify the behavior of the call. 609 | 610 | 611 | **Options** 612 | 613 | There are no options available for this call. 614 | 615 | 616 | **Parsing** 617 | 618 | To parse the results, simply step through the response structure that is detailed in the docs. For example, here's how to print the microformats: 619 | 620 | microformats('url','http://semanticweb.com/semanticweb-com-%E2%80%9Cinnovation-spotlight%E2%80%9D-interview-with-elliot-turner-ceo-of-alchemyapi_b30913', null); 622 | foreach ($response['microformats'] as $microformat) { 623 | echo 'field: ', $microformat['field'], PHP_EOL; 624 | echo 'data: ', $microformat['data'], PHP_EOL; 625 | } 626 | ?> 627 | 628 | This should print out: 629 | 630 | field: RelTagLink 631 | data: http://semanticweb.com/tag/alchemyapi 632 | field: RelTag 633 | data: alchemyapi 634 | 635 | 636 | 637 | -------------------------------------------------------------------------------- /alchemyapi.php: -------------------------------------------------------------------------------- 1 | _api_key = $key; 37 | 38 | $this->_base_url = $use_https ? $this->_BASE_HTTPS_URL : $this->_BASE_HTTP_URL; 39 | 40 | //Initialize the API Endpoints 41 | $this->_ENDPOINTS['sentiment']['url'] = '/url/URLGetTextSentiment'; 42 | $this->_ENDPOINTS['sentiment']['text'] = '/text/TextGetTextSentiment'; 43 | $this->_ENDPOINTS['sentiment']['html'] = '/html/HTMLGetTextSentiment'; 44 | $this->_ENDPOINTS['sentiment_targeted']['url'] = '/url/URLGetTargetedSentiment'; 45 | $this->_ENDPOINTS['sentiment_targeted']['text'] = '/text/TextGetTargetedSentiment'; 46 | $this->_ENDPOINTS['sentiment_targeted']['html'] = '/html/HTMLGetTargetedSentiment'; 47 | $this->_ENDPOINTS['author']['url'] = '/url/URLGetAuthor'; 48 | $this->_ENDPOINTS['author']['html'] = '/html/HTMLGetAuthor'; 49 | $this->_ENDPOINTS['keywords']['url'] = '/url/URLGetRankedKeywords'; 50 | $this->_ENDPOINTS['keywords']['text'] = '/text/TextGetRankedKeywords'; 51 | $this->_ENDPOINTS['keywords']['html'] = '/html/HTMLGetRankedKeywords'; 52 | $this->_ENDPOINTS['concepts']['url'] = '/url/URLGetRankedConcepts'; 53 | $this->_ENDPOINTS['concepts']['text'] = '/text/TextGetRankedConcepts'; 54 | $this->_ENDPOINTS['concepts']['html'] = '/html/HTMLGetRankedConcepts'; 55 | $this->_ENDPOINTS['entities']['url'] = '/url/URLGetRankedNamedEntities'; 56 | $this->_ENDPOINTS['entities']['text'] = '/text/TextGetRankedNamedEntities'; 57 | $this->_ENDPOINTS['entities']['html'] = '/html/HTMLGetRankedNamedEntities'; 58 | $this->_ENDPOINTS['category']['url'] = '/url/URLGetCategory'; 59 | $this->_ENDPOINTS['category']['text'] = '/text/TextGetCategory'; 60 | $this->_ENDPOINTS['category']['html'] = '/html/HTMLGetCategory'; 61 | $this->_ENDPOINTS['relations']['url'] = '/url/URLGetRelations'; 62 | $this->_ENDPOINTS['relations']['text'] = '/text/TextGetRelations'; 63 | $this->_ENDPOINTS['relations']['html'] = '/html/HTMLGetRelations'; 64 | $this->_ENDPOINTS['language']['url'] = '/url/URLGetLanguage'; 65 | $this->_ENDPOINTS['language']['text'] = '/text/TextGetLanguage'; 66 | $this->_ENDPOINTS['language']['html'] = '/html/HTMLGetLanguage'; 67 | $this->_ENDPOINTS['text']['url'] = '/url/URLGetText'; 68 | $this->_ENDPOINTS['text']['html'] = '/html/HTMLGetText'; 69 | $this->_ENDPOINTS['text_raw']['url'] = '/url/URLGetRawText'; 70 | $this->_ENDPOINTS['text_raw']['html'] = '/html/HTMLGetRawText'; 71 | $this->_ENDPOINTS['title']['url'] = '/url/URLGetTitle'; 72 | $this->_ENDPOINTS['title']['html'] = '/html/HTMLGetTitle'; 73 | $this->_ENDPOINTS['feeds']['url'] = '/url/URLGetFeedLinks'; 74 | $this->_ENDPOINTS['feeds']['html'] = '/html/HTMLGetFeedLinks'; 75 | $this->_ENDPOINTS['microformats']['url'] = '/url/URLGetMicroformatData'; 76 | $this->_ENDPOINTS['microformats']['html'] = '/html/HTMLGetMicroformatData'; 77 | $this->_ENDPOINTS['combined']['url'] = '/url/URLGetCombinedData'; 78 | $this->_ENDPOINTS['combined']['text'] = '/text/TextGetCombinedData'; 79 | $this->_ENDPOINTS['image']['url'] = '/url/URLGetImage'; 80 | $this->_ENDPOINTS['image_keywords']['url'] = '/url/URLGetRankedImageKeywords'; 81 | $this->_ENDPOINTS['image_keywords']['image'] = '/image/ImageGetRankedImageKeywords'; 82 | $this->_ENDPOINTS['taxonomy']['url'] = '/url/URLGetRankedTaxonomy'; 83 | $this->_ENDPOINTS['taxonomy']['html'] = '/html/HTMLGetRankedTaxonomy'; 84 | $this->_ENDPOINTS['taxonomy']['text'] = '/text/TextGetRankedTaxonomy'; 85 | } 86 | 87 | /** 88 | * Returns tag for an image URL or image included in the body of the http request. 89 | * For an overview, please refer to: http://www.alchemyapi.com/products/features/image-tagging/ 90 | * For the docs, please refer to: http://www.alchemyapi.com/api/image-tagging/ 91 | * 92 | * INPUT: 93 | * flavor -> which version of the call, i.e. url or image. 94 | * image -> the image to analyze, either the url or image data. 95 | * options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. 96 | * 97 | * Available Options: 98 | * imagePostMode -> (only applicable to image flavor) 99 | * not-raw : pass an unencoded image file with "image=URI_ENCODED_DATA" 100 | * raw : pass an unencoded image file using POST 101 | * extractMode -> 102 | * always-infer : (more CPU intensive, more accurate) 103 | * trust-metadata : (less CPU intensive, less accurate) (default) 104 | * only-metadata : (even less CPU intensive, less accurate) 105 | * 106 | * OUTPUT: 107 | * The response, already converted from JSON to a PHP object. 108 | */ 109 | public function image_keywords($flavor, $image, $options) { 110 | //Make sure this request supports the flavor 111 | if (!array_key_exists($flavor, $this->_ENDPOINTS['image_keywords'])) { 112 | return array('status'=>'ERROR','statusInfo'=>'Image tagging for ' . $flavor . ' not available'); 113 | } 114 | 115 | //Add the image to the options and analyze 116 | if($flavor=='url'){ 117 | $options[$flavor] = $image; 118 | return $this->analyze($this->_ENDPOINTS['image_keywords'][$flavor], $options); 119 | } 120 | else{ 121 | return $this->analyzeImage($this->_ENDPOINTS['image_keywords'][$flavor], $options, $image); 122 | } 123 | } 124 | 125 | 126 | /** 127 | * Extracts the entities for text, a URL or HTML. 128 | * For an overview, please refer to: http://www.alchemyapi.com/products/features/entity-extraction/ 129 | * For the docs, please refer to: http://www.alchemyapi.com/api/entity-extraction/ 130 | * 131 | * INPUT: 132 | * flavor -> which version of the call, i.e. text, url or html. 133 | * data -> the data to analyze, either the text, the url or html code. 134 | * options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. 135 | * 136 | * Available Options: 137 | * disambiguate -> disambiguate entities (i.e. Apple the company vs. apple the fruit). 0: disabled, 1: enabled (default) 138 | * linkedData -> include linked data on disambiguated entities. 0: disabled, 1: enabled (default) 139 | * coreference -> resolve coreferences (i.e. the pronouns that correspond to named entities). 0: disabled, 1: enabled (default) 140 | * quotations -> extract quotations by entities. 0: disabled (default), 1: enabled. 141 | * sentiment -> analyze sentiment for each entity. 0: disabled (default), 1: enabled. Requires 1 additional API transction if enabled. 142 | * showSourceText -> 0: disabled (default), 1: enabled 143 | * maxRetrieve -> the maximum number of entities to retrieve (default: 50) 144 | * 145 | * OUTPUT: 146 | * The response, already converted from JSON to a PHP object. 147 | */ 148 | public function entities($flavor, $data, $options) { 149 | //Make sure this request supports the flavor 150 | if (!array_key_exists($flavor, $this->_ENDPOINTS['entities'])) { 151 | return array('status'=>'ERROR','statusInfo'=>'Entity extraction for ' . $flavor . ' not available'); 152 | } 153 | 154 | //Add the data to the options and analyze 155 | $options[$flavor] = $data; 156 | return $this->analyze($this->_ENDPOINTS['entities'][$flavor], $options); 157 | } 158 | 159 | 160 | /** 161 | * Extracts the keywords from text, a URL or HTML. 162 | * For an overview, please refer to: http://www.alchemyapi.com/products/features/keyword-extraction/ 163 | * For the docs, please refer to: http://www.alchemyapi.com/api/keyword-extraction/ 164 | * 165 | * INPUT: 166 | * flavor -> which version of the call, i.e. text, url or html. 167 | * data -> the data to analyze, either the text, the url or html code. 168 | * options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. 169 | * 170 | * Available Options: 171 | * keywordExtractMode -> normal (default), strict 172 | * sentiment -> analyze sentiment for each keyword. 0: disabled (default), 1: enabled. Requires 1 additional API transaction if enabled. 173 | * showSourceText -> 0: disabled (default), 1: enabled. 174 | * maxRetrieve -> the max number of keywords returned (default: 50) 175 | * 176 | * OUTPUT: 177 | * The response, already converted from JSON to a PHP object. 178 | */ 179 | public function keywords($flavor, $data, $options) { 180 | //Make sure this request supports the flavor 181 | if (!array_key_exists($flavor, $this->_ENDPOINTS['keywords'])) { 182 | return array('status'=>'ERROR','statusInfo'=>'Keyword extraction for ' . $flavor . ' not available'); 183 | } 184 | 185 | //Add the data to the options and analyze 186 | $options[$flavor] = $data; 187 | return $this->analyze($this->_ENDPOINTS['keywords'][$flavor], $options); 188 | } 189 | 190 | 191 | /** 192 | * Tags the concepts for text, a URL or HTML. 193 | * For an overview, please refer to: http://www.alchemyapi.com/products/features/concept-tagging/ 194 | * For the docs, please refer to: http://www.alchemyapi.com/api/concept-tagging/ 195 | * 196 | * Available Options: 197 | * maxRetrieve -> the maximum number of concepts to retrieve (default: 8) 198 | * linkedData -> include linked data, 0: disabled, 1: enabled (default) 199 | * showSourceText -> 0:disabled (default), 1: enabled 200 | * 201 | * OUTPUT: 202 | * The response, already converted from JSON to a PHP object. 203 | */ 204 | public function concepts($flavor, $data, $options) { 205 | //Make sure this request supports the flavor 206 | if (!array_key_exists($flavor, $this->_ENDPOINTS['concepts'])) { 207 | return array('status'=>'ERROR','statusInfo'=>'Concept tagging for ' . $flavor . ' not available'); 208 | } 209 | 210 | //Add the data to the options and analyze 211 | $options[$flavor] = $data; 212 | return $this->analyze($this->_ENDPOINTS['concepts'][$flavor], $options); 213 | } 214 | 215 | 216 | /** 217 | * Calculates the sentiment for text, a URL or HTML. 218 | * For an overview, please refer to: http://www.alchemyapi.com/products/features/sentiment-analysis/ 219 | * For the docs, please refer to: http://www.alchemyapi.com/api/sentiment-analysis/ 220 | * 221 | * INPUT: 222 | * flavor -> which version of the call, i.e. text, url or html. 223 | * data -> the data to analyze, either the text, the url or html code. 224 | * options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. 225 | * 226 | * Available Options: 227 | * showSourceText -> 0: disabled (default), 1: enabled 228 | * 229 | * OUTPUT: 230 | * The response, already converted from JSON to a PHP object. 231 | */ 232 | public function sentiment($flavor, $data, $options) { 233 | //Make sure this request supports the flavor 234 | if (!array_key_exists($flavor, $this->_ENDPOINTS['sentiment'])) { 235 | return array('status'=>'ERROR','statusInfo'=>'Sentiment analysis for ' . $flavor . ' not available'); 236 | } 237 | 238 | //Add the data to the options and analyze 239 | $options[$flavor] = $data; 240 | return $this->analyze($this->_ENDPOINTS['sentiment'][$flavor], $options); 241 | } 242 | 243 | 244 | /** 245 | * Calculates the targeted sentiment for text, a URL or HTML. 246 | * For an overview, please refer to: http://www.alchemyapi.com/products/features/sentiment-analysis/ 247 | * For the docs, please refer to: http://www.alchemyapi.com/api/sentiment-analysis/ 248 | * 249 | * INPUT: 250 | * flavor -> which version of the call, i.e. text, url or html. 251 | * data -> the data to analyze, either the text, the url or html code. 252 | * target -> the word or phrase to run sentiment analysis on. 253 | * options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. 254 | * 255 | * Available Options: 256 | * showSourceText -> 0: disabled, 1: enabled 257 | * 258 | * OUTPUT: 259 | * The response, already converted from JSON to a PHP object. 260 | */ 261 | public function sentiment_targeted($flavor, $data, $target, $options) { 262 | //Make sure this request supports the flavor 263 | if (!array_key_exists($flavor, $this->_ENDPOINTS['sentiment_targeted'])) { 264 | return array('status'=>'ERROR','statusInfo'=>'Targeted sentiment analysis for ' . $flavor . ' not available'); 265 | } 266 | 267 | if (!$target) { 268 | return array('status'=>'ERROR','statusInfo'=>'targeted sentiment requires a non-null target'); 269 | } 270 | 271 | //Add the URL encoded data to the options and analyze 272 | $options[$flavor] = $data; 273 | $options['target'] = $target; 274 | return $this->analyze($this->_ENDPOINTS['sentiment_targeted'][$flavor], $options); 275 | } 276 | 277 | 278 | /** 279 | * Extracts the cleaned text (removes ads, navigation, etc.) for text, a URL or HTML. 280 | * For an overview, please refer to: http://www.alchemyapi.com/products/features/text-extraction/ 281 | * For the docs, please refer to: http://www.alchemyapi.com/api/text-extraction/ 282 | * 283 | * INPUT: 284 | * flavor -> which version of the call, i.e. text, url or html. 285 | * data -> the data to analyze, either the text, the url or html code. 286 | * options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. 287 | * 288 | * Available Options: 289 | * useMetadata -> utilize meta description data, 0: disabled, 1: enabled (default) 290 | * extractLinks -> include links, 0: disabled (default), 1: enabled. 291 | * 292 | * OUTPUT: 293 | * The response, already converted from JSON to a PHP object. 294 | */ 295 | public function text($flavor, $data, $options) { 296 | //Make sure this request supports the flavor 297 | if (!array_key_exists($flavor, $this->_ENDPOINTS['text'])) { 298 | return array('status'=>'ERROR','statusInfo'=>'Clean text extraction for ' . $flavor . ' not available'); 299 | } 300 | 301 | //Add the data to the options and analyze 302 | $options[$flavor] = $data; 303 | return $this->analyze($this->_ENDPOINTS['text'][$flavor], $options); 304 | } 305 | 306 | 307 | /** 308 | * Extracts the raw text (includes ads, navigation, etc.) for a URL or HTML. 309 | * For an overview, please refer to: http://www.alchemyapi.com/products/features/text-extraction/ 310 | * For the docs, please refer to: http://www.alchemyapi.com/api/text-extraction/ 311 | * 312 | * INPUT: 313 | * flavor -> which version of the call, i.e. text, url or html. 314 | * data -> the data to analyze, either the text, the url or html code. 315 | * options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. 316 | * 317 | * Available Options: 318 | * none 319 | * 320 | * OUTPUT: 321 | * The response, already converted from JSON to a PHP object. 322 | */ 323 | public function text_raw($flavor, $data, $options) { 324 | //Make sure this request supports the flavor 325 | if (!array_key_exists($flavor, $this->_ENDPOINTS['text_raw'])) { 326 | return array('status'=>'ERROR','statusInfo'=>'Raw text extraction for ' . $flavor . ' not available'); 327 | } 328 | 329 | //Add the data to the options and analyze 330 | $options[$flavor] = $data; 331 | return $this->analyze($this->_ENDPOINTS['text_raw'][$flavor], $options); 332 | } 333 | 334 | 335 | /** 336 | * Extracts the author from a URL or HTML. 337 | * For an overview, please refer to: http://www.alchemyapi.com/products/features/author-extraction/ 338 | * For the docs, please refer to: http://www.alchemyapi.com/api/author-extraction/ 339 | * 340 | * INPUT: 341 | * flavor -> which version of the call, i.e. text, url or html. 342 | * data -> the data to analyze, either the text, the url or html code. 343 | * options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. 344 | * 345 | * Availble Options: 346 | * none 347 | * 348 | * OUTPUT: 349 | * The response, already converted from JSON to a PHP object. 350 | */ 351 | public function author($flavor, $data, $options) { 352 | //Make sure this request supports the flavor 353 | if (!array_key_exists($flavor, $this->_ENDPOINTS['author'])) { 354 | return array('status'=>'ERROR','statusInfo'=>'Author extration for ' . $flavor . ' not available'); 355 | } 356 | 357 | //Add the data to the options and analyze 358 | $options[$flavor] = $data; 359 | return $this->analyze($this->_ENDPOINTS['author'][$flavor], $options); 360 | } 361 | 362 | 363 | /** 364 | * Detects the language for text, a URL or HTML. 365 | * For an overview, please refer to: http://www.alchemyapi.com/api/language-detection/ 366 | * For the docs, please refer to: http://www.alchemyapi.com/products/features/language-detection/ 367 | * 368 | * INPUT: 369 | * flavor -> which version of the call, i.e. text, url or html. 370 | * data -> the data to analyze, either the text, the url or html code. 371 | * options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. 372 | * 373 | * Available Options: 374 | * none 375 | * 376 | * OUTPUT: 377 | * The response, already converted from JSON to a PHP object. 378 | */ 379 | public function language($flavor, $data, $options) { 380 | //Make sure this request supports the flavor 381 | if (!array_key_exists($flavor, $this->_ENDPOINTS['language'])) { 382 | return array('status'=>'ERROR','statusInfo'=>'Language detection for ' . $flavor . ' not available'); 383 | } 384 | 385 | //Add the data to the options and analyze 386 | $options[$flavor] = $data; 387 | return $this->analyze($this->_ENDPOINTS['language'][$flavor], $options); 388 | } 389 | 390 | 391 | /** 392 | * Extracts the title for a URL or HTML. 393 | * For an overview, please refer to: http://www.alchemyapi.com/products/features/text-extraction/ 394 | * For the docs, please refer to: http://www.alchemyapi.com/api/text-extraction/ 395 | * 396 | * INPUT: 397 | * flavor -> which version of the call, i.e. text, url or html. 398 | * data -> the data to analyze, either the text, the url or html code. 399 | * options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. 400 | * 401 | * Available Options: 402 | * useMetadata -> utilize title info embedded in meta data, 0: disabled, 1: enabled (default) 403 | * 404 | * OUTPUT: 405 | * The response, already converted from JSON to a PHP object. 406 | */ 407 | public function title($flavor, $data, $options) { 408 | //Make sure this request supports the flavor 409 | if (!array_key_exists($flavor, $this->_ENDPOINTS['title'])) { 410 | return array('status'=>'ERROR','statusInfo'=>'Title text extraction for ' . $flavor . ' not available'); 411 | } 412 | 413 | //Add the data to the options and analyze 414 | $options[$flavor] = $data; 415 | return $this->analyze($this->_ENDPOINTS['title'][$flavor], $options); 416 | } 417 | 418 | 419 | /** 420 | * Extracts the relations for text, a URL or HTML. 421 | * For an overview, please refer to: http://www.alchemyapi.com/products/features/relation-extraction/ 422 | * For the docs, please refer to: http://www.alchemyapi.com/api/relation-extraction/ 423 | * 424 | * INPUT: 425 | * flavor -> which version of the call, i.e. text, url or html. 426 | * data -> the data to analyze, either the text, the url or html code. 427 | * options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. 428 | * 429 | * Available Options: 430 | * sentiment -> 0: disabled (default), 1: enabled. Requires one additional API transaction if enabled. 431 | * keywords -> extract keywords from the subject and object. 0: disabled (default), 1: enabled. Requires one additional API transaction if enabled. 432 | * entities -> extract entities from the subject and object. 0: disabled (default), 1: enabled. Requires one additional API transaction if enabled. 433 | * requireEntities -> only extract relations that have entities. 0: disabled (default), 1: enabled. 434 | * sentimentExcludeEntities -> exclude full entity name in sentiment analysis. 0: disabled, 1: enabled (default) 435 | * disambiguate -> disambiguate entities (i.e. Apple the company vs. apple the fruit). 0: disabled, 1: enabled (default) 436 | * linkedData -> include linked data with disambiguated entities. 0: disabled, 1: enabled (default). 437 | * coreference -> resolve entity coreferences. 0: disabled, 1: enabled (default) 438 | * showSourceText -> 0: disabled (default), 1: enabled. 439 | * maxRetrieve -> the maximum number of relations to extract (default: 50, max: 100) 440 | * 441 | * OUTPUT: 442 | * The response, already converted from JSON to a PHP object. 443 | */ 444 | public function relations($flavor, $data, $options) { 445 | //Make sure this request supports the flavor 446 | if (!array_key_exists($flavor, $this->_ENDPOINTS['relations'])) { 447 | return array('status'=>'ERROR','statusInfo'=>'Relation extraction for ' . $flavor . ' not available'); 448 | } 449 | 450 | //Add the data to the options and analyze 451 | $options[$flavor] = $data; 452 | return $this->analyze($this->_ENDPOINTS['relations'][$flavor], $options); 453 | } 454 | 455 | 456 | /** 457 | * Categorizes the text for text, a URL or HTML. 458 | * For an overview, please refer to: http://www.alchemyapi.com/products/features/text-categorization/ 459 | * For the docs, please refer to: http://www.alchemyapi.com/api/text-categorization/ 460 | * 461 | * INPUT: 462 | * flavor -> which version of the call, i.e. text, url or html. 463 | * data -> the data to analyze, either the text, the url or html code. 464 | * options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. 465 | * 466 | * Available Options: 467 | * showSourceText -> 0: disabled (default), 1: enabled 468 | * 469 | * OUTPUT: 470 | * The response, already converted from JSON to a PHP object. 471 | */ 472 | public function category($flavor, $data, $options) { 473 | //Make sure this request supports the flavor 474 | if (!array_key_exists($flavor, $this->_ENDPOINTS['category'])) { 475 | return array('status'=>'ERROR','statusInfo'=>'Text categorization for ' . $flavor . ' not available'); 476 | } 477 | 478 | //Add the data to the options and analyze 479 | $options[$flavor] = $data; 480 | return $this->analyze($this->_ENDPOINTS['category'][$flavor], $options); 481 | } 482 | 483 | 484 | /** 485 | * Detects the RSS/ATOM feeds for a URL or HTML. 486 | * For an overview, please refer to: http://www.alchemyapi.com/products/features/feed-detection/ 487 | * For the docs, please refer to: http://www.alchemyapi.com/api/feed-detection/ 488 | * 489 | * INPUT: 490 | * flavor -> which version of the call, i.e. url or html. 491 | * data -> the data to analyze, either the the url or html code. 492 | * options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. 493 | * 494 | * Available Options: 495 | * none 496 | * 497 | * OUTPUT: 498 | * The response, already converted from JSON to a PHP object. 499 | */ 500 | public function feeds($flavor, $data, $options) { 501 | //Make sure this request supports the flavor 502 | if (!array_key_exists($flavor, $this->_ENDPOINTS['feeds'])) { 503 | return array('status'=>'ERROR','statusInfo'=>'Feed detection for ' . $flavor . ' not available'); 504 | } 505 | 506 | //Add the data to the options and analyze 507 | $options[$flavor] = $data; 508 | return $this->analyze($this->_ENDPOINTS['feeds'][$flavor], $options); 509 | } 510 | 511 | 512 | /** 513 | * Parses the microformats for a URL or HTML. 514 | * For an overview, please refer to: http://www.alchemyapi.com/products/features/microformats-parsing/ 515 | * For the docs, please refer to: http://www.alchemyapi.com/api/microformats-parsing/ 516 | * 517 | * INPUT: 518 | * flavor -> which version of the call, i.e. url or html. 519 | * data -> the data to analyze, either the the url or html code. 520 | * options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. 521 | * 522 | * Available Options: 523 | * none 524 | * 525 | * OUTPUT: 526 | * The response, already converted from JSON to a PHP object. 527 | */ 528 | public function microformats($flavor, $data, $options) { 529 | //Make sure this request supports the flavor 530 | if (!array_key_exists($flavor, $this->_ENDPOINTS['microformats'])) { 531 | return array('status'=>'ERROR','statusInfo'=>'Microformat parsing for ' . $flavor . ' not available'); 532 | } 533 | 534 | //Add the data to the options and analyze 535 | $options[$flavor] = $data; 536 | return $this->analyze($this->_ENDPOINTS['microformats'][$flavor], $options); 537 | } 538 | /* 539 | Extracts main image from a URL 540 | 541 | INPUT: 542 | flavor -> which version of the call (url only currently). 543 | data -> URL to analyze 544 | options -> various parameters that can be used to adjust how the API works, 545 | see below for more info on the available options. 546 | 547 | Available Options: 548 | extractMode -> 549 | trust-metadata : (less CPU intensive, less accurate) 550 | always-infer : (more CPU intensive, more accurate) 551 | OUTPUT: 552 | The response, already converted from JSON to a Python object. 553 | */ 554 | public function imageExtraction($flavor, $data, $options) { 555 | //Make sure this request supports the flavor 556 | if (!array_key_exists($flavor, $this->_ENDPOINTS['image'])) { 557 | return array('status'=>'ERROR','statusInfo'=>'Image Extraction parsing for ' . $flavor . ' not available'); 558 | } 559 | 560 | //Add the data to the options and analyze 561 | $options[$flavor] = $data; 562 | return $this->analyze($this->_ENDPOINTS['image'][$flavor], $options); 563 | } 564 | 565 | /* 566 | Taxonomy classification operations. 567 | 568 | INPUT: 569 | flavor -> which version of the call, i.e. url or html. 570 | data -> the data to analyze, either the the url or html code. 571 | options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. 572 | 573 | 574 | Available Options: 575 | showSourceText -> 576 | include the original 'source text' the taxonomy categories were extracted from within the API response 577 | Possible values: 578 | 1 - enabled 579 | 0 - disabled (default) 580 | 581 | sourceText -> 582 | where to obtain the text that will be processed by this API call. 583 | 584 | AlchemyAPI supports multiple modes of text extraction: 585 | web page cleaning (removes ads, navigation links, etc.), raw text extraction 586 | (processes all web page text, including ads / nav links), visual constraint queries, and XPath queries. 587 | 588 | Possible values: 589 | cleaned_or_raw : cleaning enabled, fallback to raw when cleaning produces no text (default) 590 | cleaned : operate on 'cleaned' web page text (web page cleaning enabled) 591 | raw : operate on raw web page text (web page cleaning disabled) 592 | cquery : operate on the results of a visual constraints query 593 | Note: The 'cquery' http argument must also be set to a valid visual constraints query. 594 | xpath : operate on the results of an XPath query 595 | Note: The 'xpath' http argument must also be set to a valid XPath query. 596 | 597 | cquery -> 598 | a visual constraints query to apply to the web page. 599 | 600 | xpath -> 601 | an XPath query to apply to the web page. 602 | 603 | baseUrl -> 604 | rel-tag output base http url (must be uri-argument encoded) 605 | 606 | OUTPUT: 607 | The response, already converted from JSON to a Python object. 608 | 609 | */ 610 | public function taxonomy($flavor, $data, $options) { 611 | //Make sure this request supports the flavor 612 | if (!array_key_exists($flavor, $this->_ENDPOINTS['taxonomy'])) { 613 | return array('status'=>'ERROR','statusInfo'=>'taxonomy parsing for ' . $flavor . ' not available'); 614 | } 615 | 616 | //Add the data to the options and analyze 617 | $options[$flavor] = $data; 618 | return $this->analyze($this->_ENDPOINTS['taxonomy'][$flavor], $options); 619 | } 620 | 621 | /* 622 | Combined call for page-image, entity, keyword, title, author, taxonomy, concept. 623 | 624 | INPUT: 625 | flavor -> which version of the call, i.e. url or html. 626 | data -> the data to analyze, either the the url or html code. 627 | options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. 628 | 629 | Available Options: 630 | extract -> 631 | Possible values: page-image, entity, keyword, title, author, taxonomy, concept 632 | default : entity, keyword, taxonomy, concept 633 | 634 | disambiguate -> 635 | disambiguate detected entities 636 | Possible values: 637 | 1 : enabled (default) 638 | 0 : disabled 639 | 640 | linkedData -> 641 | include Linked Data content links with disambiguated entities 642 | Possible values : 643 | 1 : enabled (default) 644 | 0 : disabled 645 | 646 | coreference -> 647 | resolve he/she/etc coreferences into detected entities 648 | Possible values: 649 | 1 : enabled (default) 650 | 0 : disabled 651 | 652 | quotations -> 653 | enable quotations extraction 654 | Possible values: 655 | 1 : enabled 656 | 0 : disabled (default) 657 | 658 | sentiment -> 659 | enable entity-level sentiment analysis 660 | Possible values: 661 | 1 : enabled 662 | 0 : disabled (default) 663 | 664 | showSourceText -> 665 | include the original 'source text' the entities were extracted from within the API response 666 | Possible values: 667 | 1 : enabled 668 | 0 : disabled (default) 669 | 670 | maxRetrieve -> 671 | maximum number of named entities to extract 672 | default : 50 673 | 674 | baseUrl -> 675 | rel-tag output base http url 676 | 677 | 678 | OUTPUT: 679 | The response, already converted from JSON to a Python object. 680 | 681 | */ 682 | public function combined($flavor, $data, $options) { 683 | //Make sure this request supports the flavor 684 | if (!array_key_exists($flavor, $this->_ENDPOINTS['combined'])) { 685 | return array('status'=>'ERROR','statusInfo'=>'combined parsing for ' . $flavor . ' not available' ); 686 | } 687 | 688 | //Add the data to the options and analyze 689 | $options[$flavor] = $data; 690 | return $this->analyze($this->_ENDPOINTS['combined'][$flavor], $options); 691 | } 692 | 693 | 694 | /** 695 | * HTTP Request wrapper that is called by the endpoint functions. This function is not intended to be called through an external interface. 696 | * It makes the call, then converts the returned JSON string into a PHP object. 697 | * 698 | * INPUT: 699 | * url -> the full URI encoded url 700 | * 701 | * OUTPUT: 702 | * The response, already converted from JSON to a PHP object. 703 | */ 704 | private function analyze($endpoint, $params) { 705 | //Insert the base URL 706 | $url = $this->_base_url . $endpoint; 707 | 708 | //Add the API Key and set the output mode to JSON 709 | $params['apikey'] = $this->_api_key; 710 | $params['outputMode'] = 'json'; 711 | 712 | //Create the HTTP header 713 | $header = array('http' => array('method' => 'POST','header'=>'Content-Type: application/x-www-form-urlencode', 'content'=>http_build_query($params))); 714 | 715 | //Fire off the HTTP Request 716 | try { 717 | $fp = @fopen($url, 'rb',false, stream_context_create($header)); 718 | $response = @stream_get_contents($fp); 719 | fclose($fp); 720 | return json_decode($response, true); 721 | } catch (Exception $e) { 722 | return array('status'=>'ERROR', 'statusInfo'=>'Network error'); 723 | } 724 | } 725 | //Use to create request for image API 726 | private function analyzeImage($endpoint, $params, $imageData) { 727 | 728 | 729 | //Add the API Key and set the output mode to JSON 730 | $params['apikey'] = $this->_api_key; 731 | $params['outputMode'] = 'json'; 732 | 733 | //Insert the base URL 734 | $url = $this->_base_url . $endpoint . '?' . http_build_query($params); 735 | 736 | //Create the HTTP header 737 | $header = array('http' => array('method' => 'POST','header'=>'Content-Type: application/x-www-form-urlencode', 'content'=>$imageData)); 738 | 739 | //Fire off the HTTP Request 740 | try { 741 | $fp = @fopen($url, 'rb',false, stream_context_create($header)); 742 | $response = @stream_get_contents($fp); 743 | fclose($fp); 744 | return json_decode($response, true); 745 | } catch (Exception $e) { 746 | return array('status'=>'ERROR', 'statusInfo'=>'Network error'); 747 | } 748 | } 749 | } 750 | 751 | 752 | /** 753 | * Checks if file is called directly, and then writes the API key to api_key.txt if it's included in the args 754 | * 755 | * Note: if you don't have an API key, register for one at: http://www.alchemyapi.com/api/register.html 756 | * 757 | * INPUT: 758 | * Your API Key (sent as a command line argument) 759 | * 760 | * OUTPUT: 761 | * none 762 | */ 763 | if (php_sapi_name() == 'cli' && isset($argv) && count($argv) == 2) { 764 | if (strlen($argv[1]) == 40) { 765 | file_put_contents('api_key.txt',$argv[1]); 766 | echo 'Key: ' . $argv[1] . ' successfully written to api_key.txt', PHP_EOL; 767 | echo 'You are now ready to start using AlchemyAPI. For example, run: php example.php', PHP_EOL; 768 | } else { 769 | echo 'Invalid key! Make sure it is 40 characters in length', PHP_EOL; 770 | } 771 | } 772 | 773 | ?> 774 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "alchemyai/alchemyapi_php", 3 | "type": "sdk", 4 | "description": "A software development kit (sdk) for AlchemyAPI using PHP", 5 | "keywords": ["alchemyapi","alchemyai","php","ai","sdk"], 6 | "homepage": "http://www.alchemyapi.com/", 7 | "license": "Apache-2.0", 8 | "authors": [ 9 | { 10 | "name": "AlchemyAPI", 11 | "email": "support@alchemyapi.com", 12 | "homepage": "http://www.alchemyapi.com/", 13 | "role": "Developer" 14 | } 15 | ], 16 | "autoload": { 17 | "classmap": ["alchemyapi.php"] 18 | } 19 | } -------------------------------------------------------------------------------- /example.php: -------------------------------------------------------------------------------- 1 | PHP Demo | AlchemyAPI

Did you know that AlchemyAPI works on HTML?

Well, you do now.

'; 25 | 26 | echo PHP_EOL; 27 | echo PHP_EOL; 28 | echo ' , ', PHP_EOL; 29 | echo ' .I7777~ ', PHP_EOL; 30 | echo ' .I7777777 ', PHP_EOL; 31 | echo ' +. 77777777 ', PHP_EOL; 32 | echo ' =???, I7777777= ', PHP_EOL; 33 | echo '=?????? 7777777? ,:::===? ', PHP_EOL; 34 | echo '=???????. 777777777777777777~ .77: ?? :7 =$, :$$$$$$+ =$? ', PHP_EOL; 35 | echo ' ????????: .777777777777777777 II77 ?? :7 $$7 :$? 7$7 =$? ', PHP_EOL; 36 | echo ' .???????= +7777777777777777 .7 =7: ?? :7777+ :7:I777? ?777I= 77~777? ,777I I7 77 +$?$: :$? $$ =$? ', PHP_EOL; 37 | echo ' ???????+ ~777???+===::: :7+ ~7 ?? .77 +7 :7?. II 7~ ,I7 77+ I77 ~7 ?7 =7: .$, =$ :$? ,$$? =$? ', PHP_EOL; 38 | echo ' ,???????~ 77 7: ?? ?I. 7 :7 :7 ~7 7 77 =7: 7 7 7~ 7$ $= :$$$$$$~ =$? ', PHP_EOL; 39 | echo ' .??????? ,???I77777777777~ :77777777~ ?? 7: :7 :7 777777777:77 =7 7 +7 ~7 $$$$$$$$I :$? =$? ', PHP_EOL; 40 | echo ' .??????? ,7777777777777777 7= 77 ?? I+ 7 :7 :7 ?? 7,77 =7 7 7~ 7, =$7 $$, :$? =$? ', PHP_EOL; 41 | echo ' .???????. I77777777777777777 +7 ,7??? 77 I7 :7 :7 7~ .?7 77 =7 7 ,77I $+ 7$ :$? =$? ', PHP_EOL; 42 | echo ' ,???????= :77777777777777777~ 7= ~7?? ~I77777 :7 :7 ,777777. 77 =7 7 77, +$ .$::$? =$? ', PHP_EOL; 43 | echo ',??????? :7777777 77 ', PHP_EOL; 44 | echo ' =????? ,7777777 77= ', PHP_EOL; 45 | echo ' +?+ 7777777? ', PHP_EOL; 46 | echo ' + ~7777777 ', PHP_EOL; 47 | echo ' I777777 ', PHP_EOL; 48 | echo ' :~ ', PHP_EOL; 49 | 50 | 51 | echo PHP_EOL; 52 | echo PHP_EOL; 53 | echo '############################################', PHP_EOL; 54 | echo '# Image Keyword Example #', PHP_EOL; 55 | echo '############################################', PHP_EOL; 56 | echo PHP_EOL; 57 | echo PHP_EOL; 58 | 59 | echo 'Processing Image URL: ', $demo_url, PHP_EOL; 60 | echo PHP_EOL; 61 | 62 | $response = $alchemyapi->image_keywords('url', $demo_url, array('extractMode'=>'trust-metadata')); 63 | 64 | if ($response['status'] == 'OK') { 65 | echo '## Response Object ##', PHP_EOL; 66 | echo print_r($response); 67 | 68 | echo PHP_EOL; 69 | echo '## Image Keywords ##', PHP_EOL; 70 | foreach ($response['imageKeywords'] as $imageKeywords) { 71 | echo 'image keyword: ', $imageKeywords['text'], PHP_EOL; 72 | echo 'score: ', $imageKeywords['score'], PHP_EOL; 73 | echo PHP_EOL; 74 | } 75 | } else { 76 | echo 'Error in the image keyword extraction call: ', $response['statusInfo']; 77 | } 78 | echo PHP_EOL; 79 | echo PHP_EOL; 80 | echo PHP_EOL; 81 | echo PHP_EOL; 82 | /* 83 | $imageName = "grumpy-cat-meme-hmmm.jpg"; 84 | $imageFile = fopen($imageName, "r") or die("Unable to open file!"); 85 | $imageData = fread($imageFile,filesize($imageName)); 86 | fclose($imageFile); 87 | 88 | 89 | echo PHP_EOL; 90 | echo PHP_EOL; 91 | echo '############################################', PHP_EOL; 92 | echo '# Image Keyword Example with image #', PHP_EOL; 93 | echo '############################################', PHP_EOL; 94 | echo PHP_EOL; 95 | echo PHP_EOL; 96 | 97 | echo 'Processing Image File: ', $imageName, PHP_EOL; 98 | echo PHP_EOL; 99 | 100 | $response = $alchemyapi->image_keywords('image', $imageData, array('imagePostMode'=>'raw')); 101 | 102 | if ($response['status'] == 'OK') { 103 | echo '## Response Object ##', PHP_EOL; 104 | echo print_r($response); 105 | 106 | echo PHP_EOL; 107 | echo '## Image Keywords ##', PHP_EOL; 108 | foreach ($response['imageKeywords'] as $imageKeywords) { 109 | echo 'image keyword: ', $imageKeywords['text'], PHP_EOL; 110 | echo 'score: ', $imageKeywords['score'], PHP_EOL; 111 | echo PHP_EOL; 112 | } 113 | } else { 114 | echo 'Error in the image keyword extraction call: ', $response['statusInfo']; 115 | } 116 | echo PHP_EOL; 117 | echo PHP_EOL;*/ 118 | echo PHP_EOL; 119 | echo PHP_EOL; 120 | echo PHP_EOL; 121 | echo '############################################', PHP_EOL; 122 | echo '# Entity Extraction Example #', PHP_EOL; 123 | echo '############################################', PHP_EOL; 124 | echo PHP_EOL; 125 | echo PHP_EOL; 126 | 127 | echo 'Processing text: ', $demo_text, PHP_EOL; 128 | echo PHP_EOL; 129 | 130 | $response = $alchemyapi->entities('text',$demo_text, array('sentiment'=>1)); 131 | 132 | if ($response['status'] == 'OK') { 133 | echo '## Response Object ##', PHP_EOL; 134 | echo print_r($response); 135 | 136 | echo PHP_EOL; 137 | echo '## Entities ##', PHP_EOL; 138 | foreach ($response['entities'] as $entity) { 139 | echo 'entity: ', $entity['text'], PHP_EOL; 140 | echo 'type: ', $entity['type'], PHP_EOL; 141 | echo 'relevance: ', $entity['relevance'], PHP_EOL; 142 | echo 'sentiment: ', $entity['sentiment']['type']; 143 | if (array_key_exists('score', $entity['sentiment'])) { 144 | echo ' (' . $entity['sentiment']['score'] . ')', PHP_EOL; 145 | } else { 146 | echo PHP_EOL; 147 | } 148 | 149 | echo PHP_EOL; 150 | } 151 | } else { 152 | echo 'Error in the entity extraction call: ', $response['statusInfo']; 153 | } 154 | 155 | 156 | echo PHP_EOL; 157 | echo PHP_EOL; 158 | echo PHP_EOL; 159 | echo '############################################', PHP_EOL; 160 | echo '# Keyword Extraction Example #', PHP_EOL; 161 | echo '############################################', PHP_EOL; 162 | echo PHP_EOL; 163 | echo PHP_EOL; 164 | 165 | echo 'Processing text: ', $demo_text, PHP_EOL; 166 | echo PHP_EOL; 167 | 168 | $response = $alchemyapi->keywords('text',$demo_text, array('sentiment'=>1)); 169 | 170 | if ($response['status'] == 'OK') { 171 | echo '## Response Object ##', PHP_EOL; 172 | echo print_r($response); 173 | 174 | echo PHP_EOL; 175 | echo '## Keywords ##', PHP_EOL; 176 | foreach ($response['keywords'] as $keyword) { 177 | echo 'keyword: ', $keyword['text'], PHP_EOL; 178 | echo 'relevance: ', $keyword['relevance'], PHP_EOL; 179 | echo 'sentiment: ', $keyword['sentiment']['type']; 180 | if (array_key_exists('score', $keyword['sentiment'])) { 181 | echo ' (' . $keyword['sentiment']['score'] . ')', PHP_EOL; 182 | } else { 183 | echo PHP_EOL; 184 | } 185 | echo PHP_EOL; 186 | } 187 | } else { 188 | echo 'Error in the keyword extraction call: ', $response['statusInfo']; 189 | } 190 | 191 | 192 | echo PHP_EOL; 193 | echo PHP_EOL; 194 | echo PHP_EOL; 195 | echo '############################################', PHP_EOL; 196 | echo '# Concept Tagging Example #', PHP_EOL; 197 | echo '############################################', PHP_EOL; 198 | echo PHP_EOL; 199 | echo PHP_EOL; 200 | 201 | echo 'Processing text: ', $demo_text, PHP_EOL; 202 | echo PHP_EOL; 203 | 204 | $response = $alchemyapi->concepts('text',$demo_text, null); 205 | 206 | if ($response['status'] == 'OK') { 207 | echo '## Response Object ##', PHP_EOL; 208 | echo print_r($response); 209 | 210 | echo PHP_EOL; 211 | echo '## Concepts ##', PHP_EOL; 212 | foreach ($response['concepts'] as $concept) { 213 | echo 'concept: ', $concept['text'], PHP_EOL; 214 | echo 'relevance: ', $concept['relevance'], PHP_EOL; 215 | echo PHP_EOL; 216 | } 217 | } else { 218 | echo 'Error in the concept tagging call: ', $response['statusInfo']; 219 | } 220 | 221 | 222 | echo PHP_EOL; 223 | echo PHP_EOL; 224 | echo PHP_EOL; 225 | echo '############################################', PHP_EOL; 226 | echo '# Sentiment Analysis Example #', PHP_EOL; 227 | echo '############################################', PHP_EOL; 228 | echo PHP_EOL; 229 | echo PHP_EOL; 230 | 231 | echo 'Processing HTML: ', $demo_html, PHP_EOL; 232 | echo PHP_EOL; 233 | 234 | $response = $alchemyapi->sentiment('html',$demo_html, null); 235 | 236 | if ($response['status'] == 'OK') { 237 | echo '## Response Object ##', PHP_EOL; 238 | echo print_r($response); 239 | 240 | echo PHP_EOL; 241 | echo '## Document Sentiment ##', PHP_EOL; 242 | echo 'sentiment: ', $response['docSentiment']['type'], PHP_EOL; 243 | if (array_key_exists('score', $response['docSentiment'])) { 244 | echo 'score: ', $response['docSentiment']['score'], PHP_EOL; 245 | } 246 | } else { 247 | echo 'Error in the sentiment analysis call: ', $response['statusInfo']; 248 | } 249 | 250 | 251 | echo PHP_EOL; 252 | echo PHP_EOL; 253 | echo PHP_EOL; 254 | echo '############################################', PHP_EOL; 255 | echo '# Targeted Sentiment Analysis Example #', PHP_EOL; 256 | echo '############################################', PHP_EOL; 257 | echo PHP_EOL; 258 | echo PHP_EOL; 259 | 260 | echo 'Processing text: ', $demo_text, PHP_EOL; 261 | echo 'Target: Denver, Colorado', PHP_EOL; 262 | echo PHP_EOL; 263 | 264 | $response = $alchemyapi->sentiment_targeted('text',$demo_text,'Denver', null); 265 | 266 | if ($response['status'] == 'OK') { 267 | echo '## Response Object ##', PHP_EOL; 268 | echo print_r($response); 269 | 270 | echo PHP_EOL; 271 | echo '## Targeted Sentiment ##', PHP_EOL; 272 | echo 'sentiment: ', $response['docSentiment']['type'], PHP_EOL; 273 | if (array_key_exists('score', $response['docSentiment'])) { 274 | echo 'score: ', $response['docSentiment']['score'], PHP_EOL; 275 | } 276 | } else { 277 | echo 'Error in the targeted sentiment analysis call: ', $response['statusInfo']; 278 | } 279 | 280 | 281 | echo PHP_EOL; 282 | echo PHP_EOL; 283 | echo PHP_EOL; 284 | echo '############################################', PHP_EOL; 285 | echo '# Text Extraction Example #', PHP_EOL; 286 | echo '############################################', PHP_EOL; 287 | echo PHP_EOL; 288 | echo PHP_EOL; 289 | 290 | echo 'Processing url: ', $demo_url, PHP_EOL; 291 | echo PHP_EOL; 292 | 293 | $response = $alchemyapi->text('url', $demo_url, null); 294 | 295 | if ($response['status'] == 'OK') { 296 | echo '## Response Object ##', PHP_EOL; 297 | echo print_r($response); 298 | 299 | echo PHP_EOL; 300 | echo '## Extracted Text ##', PHP_EOL; 301 | echo 'text: ',PHP_EOL, $response['text'], PHP_EOL; 302 | } else { 303 | echo 'Error in the text extraction call: ', $response['statusInfo']; 304 | } 305 | 306 | 307 | echo PHP_EOL; 308 | echo PHP_EOL; 309 | echo PHP_EOL; 310 | echo '############################################', PHP_EOL; 311 | echo '# Author Extraction Example #', PHP_EOL; 312 | echo '############################################', PHP_EOL; 313 | echo PHP_EOL; 314 | echo PHP_EOL; 315 | 316 | echo 'Processing url: ', $demo_url, PHP_EOL; 317 | echo PHP_EOL; 318 | 319 | $response = $alchemyapi->author('url',$demo_url, null); 320 | 321 | if ($response['status'] == 'OK') { 322 | echo '## Response Object ##', PHP_EOL; 323 | echo print_r($response); 324 | 325 | echo PHP_EOL; 326 | echo '## Author ##', PHP_EOL; 327 | echo 'author: ', $response['author'], PHP_EOL; 328 | } else { 329 | echo 'Error in the author extraction call: ', $response['statusInfo']; 330 | } 331 | 332 | 333 | echo PHP_EOL; 334 | echo PHP_EOL; 335 | echo PHP_EOL; 336 | echo '############################################', PHP_EOL; 337 | echo '# Language Detection Example #', PHP_EOL; 338 | echo '############################################', PHP_EOL; 339 | echo PHP_EOL; 340 | echo PHP_EOL; 341 | 342 | echo 'Processing text: ', $demo_text, PHP_EOL; 343 | echo PHP_EOL; 344 | 345 | $response = $alchemyapi->language('text',$demo_text, null); 346 | 347 | if ($response['status'] == 'OK') { 348 | echo '## Response Object ##', PHP_EOL; 349 | echo print_r($response); 350 | 351 | echo PHP_EOL; 352 | echo '## Language ##', PHP_EOL; 353 | echo 'language: ', $response['language'], PHP_EOL; 354 | echo 'iso-639-1: ', $response['iso-639-1'], PHP_EOL; 355 | echo 'native speakers: ', $response['native-speakers'], PHP_EOL; 356 | } else { 357 | echo 'Error in the language detection call: ', $response['statusInfo']; 358 | } 359 | 360 | 361 | echo PHP_EOL; 362 | echo PHP_EOL; 363 | echo PHP_EOL; 364 | echo '############################################', PHP_EOL; 365 | echo '# Title Extraction Example #', PHP_EOL; 366 | echo '############################################', PHP_EOL; 367 | echo PHP_EOL; 368 | echo PHP_EOL; 369 | 370 | echo 'Processing url: ', $demo_url, PHP_EOL; 371 | echo PHP_EOL; 372 | 373 | $response = $alchemyapi->title('url',$demo_url, null); 374 | 375 | if ($response['status'] == 'OK') { 376 | echo '## Response Object ##', PHP_EOL; 377 | echo print_r($response); 378 | 379 | echo PHP_EOL; 380 | echo '## Title ##', PHP_EOL; 381 | echo 'title: ', $response['title'], PHP_EOL; 382 | } else { 383 | echo 'Error in the title extraction call: ', $response['statusInfo']; 384 | } 385 | 386 | 387 | echo PHP_EOL; 388 | echo PHP_EOL; 389 | echo PHP_EOL; 390 | echo '############################################', PHP_EOL; 391 | echo '# Relation Extraction Example #', PHP_EOL; 392 | echo '############################################', PHP_EOL; 393 | echo PHP_EOL; 394 | echo PHP_EOL; 395 | 396 | echo 'Processing text: ', $demo_text, PHP_EOL; 397 | echo PHP_EOL; 398 | 399 | $response = $alchemyapi->relations('text',$demo_text, null); 400 | 401 | if ($response['status'] == 'OK') { 402 | echo '## Response Object ##', PHP_EOL; 403 | echo print_r($response); 404 | 405 | echo PHP_EOL; 406 | echo '## Relations ##', PHP_EOL; 407 | foreach ($response['relations'] as $relation) { 408 | if (array_key_exists('subject', $relation)) { 409 | echo 'Subject: ', $relation['subject']['text'], PHP_EOL; 410 | } 411 | 412 | if (array_key_exists('action', $relation)) { 413 | echo 'Action: ', $relation['action']['text'], PHP_EOL; 414 | } 415 | 416 | if (array_key_exists('object', $relation)) { 417 | echo 'Object: ', $relation['object']['text'], PHP_EOL; 418 | } 419 | echo PHP_EOL; 420 | } 421 | } else { 422 | echo 'Error in the relation extraction call: ', $response['statusInfo']; 423 | } 424 | 425 | 426 | echo PHP_EOL; 427 | echo PHP_EOL; 428 | echo PHP_EOL; 429 | echo '############################################', PHP_EOL; 430 | echo '# Text Categorization Example #', PHP_EOL; 431 | echo '############################################', PHP_EOL; 432 | echo PHP_EOL; 433 | echo PHP_EOL; 434 | 435 | echo 'Processing text: ', $demo_text, PHP_EOL; 436 | echo PHP_EOL; 437 | 438 | $response = $alchemyapi->category('text',$demo_text, null); 439 | 440 | if ($response['status'] == 'OK') { 441 | echo '## Response Object ##', PHP_EOL; 442 | echo print_r($response); 443 | 444 | echo PHP_EOL; 445 | echo '## Category ##', PHP_EOL; 446 | echo 'category: ', $response['category'], PHP_EOL; 447 | echo 'score: ', $response['score'], PHP_EOL; 448 | } else { 449 | echo 'Error in the text categorization call: ', $response['statusInfo']; 450 | } 451 | 452 | 453 | echo PHP_EOL; 454 | echo PHP_EOL; 455 | echo PHP_EOL; 456 | echo '############################################', PHP_EOL; 457 | echo '# Feed Detection Example #', PHP_EOL; 458 | echo '############################################', PHP_EOL; 459 | echo PHP_EOL; 460 | echo PHP_EOL; 461 | 462 | echo 'Processing url: ', $demo_url, PHP_EOL; 463 | echo PHP_EOL; 464 | 465 | $response = $alchemyapi->feeds('url',$demo_url, null); 466 | 467 | if ($response['status'] == 'OK') { 468 | echo '## Response Object ##', PHP_EOL; 469 | echo print_r($response); 470 | 471 | echo PHP_EOL; 472 | echo '## Feeds ##', PHP_EOL; 473 | foreach ($response['feeds'] as $feed) { 474 | echo 'feed: ', $feed['feed'], PHP_EOL; 475 | } 476 | } else { 477 | echo 'Error in the feed detection call: ', $response['statusInfo']; 478 | } 479 | 480 | 481 | echo PHP_EOL; 482 | echo PHP_EOL; 483 | echo PHP_EOL; 484 | echo '############################################', PHP_EOL; 485 | echo '# Microformats Parsing Example #', PHP_EOL; 486 | echo '############################################', PHP_EOL; 487 | echo PHP_EOL; 488 | echo PHP_EOL; 489 | 490 | echo 'Processing url: ', $demo_url, PHP_EOL; 491 | echo PHP_EOL; 492 | 493 | $response = $alchemyapi->microformats('url',$demo_url, null); 494 | 495 | if ($response['status'] == 'OK') { 496 | echo '## Response Object ##', PHP_EOL; 497 | echo print_r($response); 498 | 499 | echo PHP_EOL; 500 | echo '## Microformats ##', PHP_EOL; 501 | foreach ($response['microformats'] as $microformat) { 502 | echo 'field: ', $microformat['field'], PHP_EOL; 503 | echo 'data: ', $microformat['data'], PHP_EOL, PHP_EOL; 504 | } 505 | } else { 506 | echo 'Error in the microformat parsing call: ', $response['statusInfo']; 507 | } 508 | 509 | 510 | echo PHP_EOL; 511 | echo PHP_EOL; 512 | 513 | echo PHP_EOL; 514 | echo PHP_EOL; 515 | echo PHP_EOL; 516 | echo '############################################', PHP_EOL; 517 | echo '# Image Extraction Example #', PHP_EOL; 518 | echo '############################################', PHP_EOL; 519 | echo PHP_EOL; 520 | echo PHP_EOL; 521 | 522 | echo 'Processing url: ', $demo_url, PHP_EOL; 523 | echo PHP_EOL; 524 | 525 | $response = $alchemyapi->imageExtraction('url',$demo_url, null); 526 | 527 | if ($response['status'] == 'OK') { 528 | echo '## Response Object ##', PHP_EOL; 529 | echo print_r($response); 530 | 531 | echo PHP_EOL; 532 | echo '## Image ##', PHP_EOL; 533 | echo 'Image: ', $response['image'], PHP_EOL; 534 | } else { 535 | echo 'Error in the image extraction call: ', $response['statusInfo']; 536 | } 537 | 538 | 539 | echo PHP_EOL; 540 | echo PHP_EOL; 541 | 542 | echo PHP_EOL; 543 | echo PHP_EOL; 544 | echo PHP_EOL; 545 | echo '############################################', PHP_EOL; 546 | echo '# taxonomy Example #', PHP_EOL; 547 | echo '############################################', PHP_EOL; 548 | echo PHP_EOL; 549 | echo PHP_EOL; 550 | 551 | echo 'Processing text: ', $demo_text, PHP_EOL; 552 | echo PHP_EOL; 553 | 554 | $response = $alchemyapi->taxonomy('text',$demo_text, null); 555 | 556 | if ($response['status'] == 'OK') { 557 | echo '## Response Object ##', PHP_EOL; 558 | echo print_r($response); 559 | 560 | echo PHP_EOL; 561 | echo '## Categories ##', PHP_EOL; 562 | foreach ($response['taxonomy'] as $category) { 563 | echo $category['label'], ' : ', $category['score'], PHP_EOL; 564 | } 565 | } else { 566 | echo 'Error in the taxonomy call: ', $response['statusInfo']; 567 | } 568 | 569 | echo PHP_EOL; 570 | echo PHP_EOL; 571 | 572 | echo PHP_EOL; 573 | echo PHP_EOL; 574 | echo PHP_EOL; 575 | echo '############################################', PHP_EOL; 576 | echo '# combined Example #', PHP_EOL; 577 | echo '############################################', PHP_EOL; 578 | echo PHP_EOL; 579 | echo PHP_EOL; 580 | 581 | echo 'Processing text: ', $demo_text, PHP_EOL; 582 | echo PHP_EOL; 583 | 584 | $response = $alchemyapi->combined('text',$demo_text, null); 585 | 586 | if ($response['status'] == 'OK') { 587 | echo '## Response Object ##', PHP_EOL; 588 | echo print_r($response); 589 | 590 | echo PHP_EOL; 591 | 592 | echo '## Keywords ##', PHP_EOL; 593 | foreach ($response['keywords'] as $keyword) { 594 | echo $keyword['text'], ' : ', $keyword['relevance'], PHP_EOL; 595 | } 596 | echo PHP_EOL; 597 | 598 | echo '## Concepts ##', PHP_EOL; 599 | foreach ($response['concepts'] as $concept) { 600 | echo $concept['text'], ' : ', $concept['relevance'], PHP_EOL; 601 | } 602 | echo PHP_EOL; 603 | 604 | echo '## Entities ##', PHP_EOL; 605 | foreach ($response['entities'] as $entity) { 606 | echo $entity['type'], ' : ', $entity['text'], ' , ', $entity['relevance'], PHP_EOL; 607 | } 608 | echo PHP_EOL; 609 | } else { 610 | echo 'Error in the taxonomy call: ', $response['statusInfo']; 611 | } 612 | 613 | echo PHP_EOL; 614 | echo PHP_EOL; 615 | 616 | ?> 617 | -------------------------------------------------------------------------------- /grumpy-cat-meme-hmmm.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlchemyAPI/alchemyapi_php/56e2b65ca14ac5d1d979d434246e09797bf556eb/grumpy-cat-meme-hmmm.jpg -------------------------------------------------------------------------------- /tests.php: -------------------------------------------------------------------------------- 1 | The best SDK Test | AlchemyAPI

Hello World!

My favorite language is PHP

'; 25 | $test_url = 'http://www.nytimes.com/2013/07/13/us/politics/a-day-of-friction-notable-even-for-a-fractious-congress.html?_r=0'; 26 | $imageName = "grumpy-cat-meme-hmmm.jpg"; 27 | $imageFile = fopen($imageName, "r") or die("Unable to open file!"); 28 | $imageData = fread($imageFile,filesize($imageName)); 29 | fclose($imageFile); 30 | 31 | //image keywords 32 | echo 'Checking image keywords . . . ', PHP_EOL; 33 | $response = $alchemyapi->image_keywords('url', $test_url, null); 34 | assert($response['status'] == 'OK'); 35 | $response = $alchemyapi->image_keywords('image', $imageData, array('imagePostMode'=>'raw')); 36 | assert($response['status'] == 'OK'); 37 | $response = $alchemyapi->image_keywords('random', $test_url, null); 38 | assert($response['status'] == 'ERROR'); //invalid flavor 39 | echo 'Image keyword tests complete!', PHP_EOL, PHP_EOL; 40 | 41 | //image extraction 42 | echo 'Checking image extraction . . . ', PHP_EOL; 43 | $response = $alchemyapi->imageExtraction('url',$test_url, null); 44 | assert($response['status'] == 'OK'); 45 | $response = $alchemyapi->imageExtraction('random', $test_url, null); 46 | assert($response['status'] == 'ERROR'); //invalid flavor 47 | echo 'Image extraction tests complete!', PHP_EOL, PHP_EOL; 48 | 49 | //taxonomy 50 | echo 'Checking Taxonomy . . . ', PHP_EOL; 51 | $response = $alchemyapi->taxonomy('text',$test_text, null); 52 | assert($response['status'] == 'OK'); 53 | $response = $alchemyapi->taxonomy('random', $test_text, null); 54 | assert($response['status'] == 'ERROR'); //invalid flavor 55 | echo 'Taxonomy tests complete!', PHP_EOL, PHP_EOL; 56 | 57 | //combined 58 | echo 'Checking Combined . . . ', PHP_EOL; 59 | $response = $alchemyapi->combined('text',$test_text, null); 60 | assert($response['status'] == 'OK'); 61 | $response = $alchemyapi->combined('random', $test_text, null); 62 | assert($response['status'] == 'ERROR'); //invalid flavor 63 | echo 'Combined tests complete!', PHP_EOL, PHP_EOL; 64 | 65 | //Entities 66 | echo 'Checking entities . . . ', PHP_EOL; 67 | $response = $alchemyapi->entities('text', $test_text, null); 68 | assert($response['status'] == 'OK'); 69 | $response = $alchemyapi->entities('html', $test_html, null); 70 | assert($response['status'] == 'OK'); 71 | $response = $alchemyapi->entities('url', $test_url, null); 72 | assert($response['status'] == 'OK'); 73 | $response = $alchemyapi->entities('random', $test_url, null); 74 | assert($response['status'] == 'ERROR'); //invalid flavor 75 | echo 'Entity tests complete!', PHP_EOL, PHP_EOL; 76 | 77 | 78 | //Keywords 79 | echo 'Checking keywords . . . ', PHP_EOL; 80 | $response = $alchemyapi->keywords('text', $test_text, null); 81 | assert($response['status'] == 'OK'); 82 | $response = $alchemyapi->keywords('html', $test_html, null); 83 | assert($response['status'] == 'OK'); 84 | $response = $alchemyapi->keywords('url', $test_url, null); 85 | assert($response['status'] == 'OK'); 86 | $response = $alchemyapi->keywords('random', $test_url, null); 87 | assert($response['status'] == 'ERROR'); //invalid flavor 88 | echo 'Keyword tests complete!', PHP_EOL, PHP_EOL; 89 | 90 | 91 | //Concepts 92 | echo 'Checking concepts . . . ', PHP_EOL; 93 | $response = $alchemyapi->concepts('text', $test_text, null); 94 | assert($response['status'] == 'OK'); 95 | $response = $alchemyapi->concepts('html', $test_html, null); 96 | assert($response['status'] == 'OK'); 97 | $response = $alchemyapi->concepts('url', $test_url, null); 98 | assert($response['status'] == 'OK'); 99 | $response = $alchemyapi->concepts('random', $test_url, null); 100 | assert($response['status'] == 'ERROR'); //invalid flavor 101 | echo 'Concept tests complete!', PHP_EOL, PHP_EOL; 102 | 103 | 104 | //Sentiment 105 | echo 'Checking sentiment . . . ', PHP_EOL; 106 | $response = $alchemyapi->sentiment('text', $test_text, null); 107 | assert($response['status'] == 'OK'); 108 | $response = $alchemyapi->sentiment('html', $test_html, null); 109 | assert($response['status'] == 'OK'); 110 | $response = $alchemyapi->sentiment('url', $test_url, null); 111 | assert($response['status'] == 'OK'); 112 | $response = $alchemyapi->sentiment('random', $test_url, null); 113 | assert($response['status'] == 'ERROR'); //invalid flavor 114 | echo 'Sentiment tests complete!', PHP_EOL, PHP_EOL; 115 | 116 | 117 | //Sentiment Targeted 118 | echo 'Checking targeted sentiment . . . ', PHP_EOL; 119 | $response = $alchemyapi->sentiment_targeted('text', $test_text, 'heart', null); 120 | assert($response['status'] == 'OK'); 121 | $response = $alchemyapi->sentiment_targeted('html', $test_html, 'language', null); 122 | assert($response['status'] == 'OK'); 123 | $response = $alchemyapi->sentiment_targeted('url', $test_url, 'Congress', null); 124 | assert($response['status'] == 'OK'); 125 | $response = $alchemyapi->sentiment_targeted('random', $test_url, 'Congress', null); 126 | assert($response['status'] == 'ERROR'); //invalid flavor 127 | $response = $alchemyapi->sentiment_targeted('text', $test_text, null, null); 128 | assert($response['status'] == 'ERROR'); //missing target 129 | echo 'Targeted sentiment tests complete!', PHP_EOL, PHP_EOL; 130 | 131 | 132 | //Text 133 | echo 'Checking clean text . . . ', PHP_EOL; 134 | $response = $alchemyapi->text('text', $test_text, null); 135 | assert($response['status'] == 'ERROR'); //text only works on html and url content 136 | $response = $alchemyapi->text('html', $test_html, null); 137 | assert($response['status'] == 'OK'); 138 | $response = $alchemyapi->text('url', $test_url, null); 139 | assert($response['status'] == 'OK'); 140 | echo 'Clean text tests complete!', PHP_EOL, PHP_EOL; 141 | 142 | 143 | //Text Raw 144 | echo 'Checking raw text . . . ', PHP_EOL; 145 | $response = $alchemyapi->text_raw('text', $test_text, null); 146 | assert($response['status'] == 'ERROR'); //text_raw only works on html and url content 147 | $response = $alchemyapi->text_raw('html', $test_html, null); 148 | assert($response['status'] == 'OK'); 149 | $response = $alchemyapi->text_raw('url', $test_url, null); 150 | assert($response['status'] == 'OK'); 151 | echo 'Raw text tests complete!', PHP_EOL, PHP_EOL; 152 | 153 | 154 | //Author 155 | echo 'Checking author . . . ', PHP_EOL; 156 | $response = $alchemyapi->author('text', $test_text, null); 157 | assert($response['status'] == 'ERROR'); //author only works on html and url content 158 | $response = $alchemyapi->author('html', $test_html, null); 159 | assert($response['status'] == 'ERROR'); //there is no author listed in the test HTML content 160 | $response = $alchemyapi->author('url', $test_url, null); 161 | assert($response['status'] == 'OK'); 162 | echo 'Author tests complete!', PHP_EOL, PHP_EOL; 163 | 164 | 165 | //Language 166 | echo 'Checking language . . . ', PHP_EOL; 167 | $response = $alchemyapi->language('text', $test_text, null); 168 | assert($response['status'] == 'OK'); 169 | $response = $alchemyapi->language('html', $test_html, null); 170 | assert($response['status'] == 'OK'); 171 | $response = $alchemyapi->language('url', $test_url, null); 172 | assert($response['status'] == 'OK'); 173 | $response = $alchemyapi->language('random', $test_url, null); 174 | assert($response['status'] == 'ERROR'); //invalid flavor 175 | echo 'Language tests complete!', PHP_EOL, PHP_EOL; 176 | 177 | 178 | //Title 179 | echo 'Checking title . . . ', PHP_EOL; 180 | $response = $alchemyapi->title('text', $test_text, null); 181 | assert($response['status'] == 'ERROR'); //title only works on html and url content 182 | $response = $alchemyapi->title('html', $test_html, null); 183 | assert($response['status'] == 'OK'); 184 | $response = $alchemyapi->title('url', $test_url, null); 185 | assert($response['status'] == 'OK'); 186 | echo 'Title tests complete!', PHP_EOL, PHP_EOL; 187 | 188 | 189 | //Relations 190 | echo 'Checking relations . . . ', PHP_EOL; 191 | $response = $alchemyapi->relations('text', $test_text, null); 192 | assert($response['status'] == 'OK'); 193 | $response = $alchemyapi->relations('html', $test_html, null); 194 | assert($response['status'] == 'OK'); 195 | $response = $alchemyapi->relations('url', $test_url, null); 196 | assert($response['status'] == 'OK'); 197 | $response = $alchemyapi->relations('random', $test_url, null); 198 | assert($response['status'] == 'ERROR'); //invalid flavor 199 | echo 'Relations tests complete!', PHP_EOL, PHP_EOL; 200 | 201 | 202 | //Category 203 | echo 'Checking category . . . ', PHP_EOL; 204 | $response = $alchemyapi->category('text', $test_text, null); 205 | assert($response['status'] == 'OK'); 206 | $response = $alchemyapi->category('html', $test_html, array('url'=>'test')); 207 | assert($response['status'] == 'OK'); 208 | $response = $alchemyapi->category('url', $test_url, null); 209 | assert($response['status'] == 'OK'); 210 | $response = $alchemyapi->category('random', $test_url, null); 211 | assert($response['status'] == 'ERROR'); //invalid flavor 212 | echo 'Category tests complete!', PHP_EOL, PHP_EOL; 213 | 214 | 215 | //Feeds 216 | echo 'Checking feeds . . . ', PHP_EOL; 217 | $response = $alchemyapi->feeds('text', $test_text, null); 218 | assert($response['status'] == 'ERROR'); //feeds only works on html and url content 219 | $response = $alchemyapi->feeds('html', $test_html, array('url'=>'test')); 220 | assert($response['status'] == 'OK'); 221 | $response = $alchemyapi->feeds('url', $test_url, null); 222 | assert($response['status'] == 'OK'); 223 | echo 'Feed tests complete!', PHP_EOL, PHP_EOL; 224 | 225 | 226 | //Microformats 227 | echo 'Checking microformats . . . ', PHP_EOL; 228 | $response = $alchemyapi->microformats('text', $test_text, null); 229 | assert($response['status'] == 'ERROR'); //microformats only works on html and url content 230 | $response = $alchemyapi->microformats('html', $test_html, array('url'=>'test')); 231 | assert($response['status'] == 'OK'); 232 | $response = $alchemyapi->microformats('url', $test_url, null); 233 | assert($response['status'] == 'OK'); 234 | echo 'Microformat tests complete!', PHP_EOL, PHP_EOL; 235 | 236 | echo '**** All tests are complete! ****', PHP_EOL; 237 | ?> 238 | --------------------------------------------------------------------------------