It get the like count of Facebook.
Dependencies: EthernetInterface HTTPClient mbed-rtos mbed
Fork of HTTPClient_HelloWorld by
Diff: main.cpp
- Revision:
- 3:7ad266ae429f
- Parent:
- 2:270e2d0bb85a
--- a/main.cpp Thu Aug 30 15:42:06 2012 +0000 +++ b/main.cpp Sun Dec 22 05:43:22 2013 +0000 @@ -1,79 +1,67 @@ #include "mbed.h" #include "EthernetInterface.h" #include "HTTPClient.h" +#include <string.h> +#include <ctype.h> + +#define URL "https://www.facebook.com/mbedmicro" EthernetInterface eth; -HTTPClient http; -char str[512]; + +int urlencode (char *str, char *buf, int len); + +int facebookLikes (char *target) { + HTTPClient http; + char url[256], buf[1024]; + char *s; + + strcpy(url, "http://graph.facebook.com/"); + urlencode(target, &url[strlen(url)], sizeof(url) - strlen(url)); +// printf("url: %s\r\n", url); + + if (http.get(url, buf, sizeof(buf))) return -1; + + s = strstr(buf, "\"likes\":"); + if (s == NULL) return -1; + s += 8; + return atoi(s); +} int main() { eth.init(); //Use DHCP + eth.connect(); + printf("IP: %s\r\n", eth.getIPAddress()); - eth.connect(); - - //GET data - printf("\nTrying to fetch page...\n"); - int ret = http.get("http://mbed.org/media/uploads/donatien/hello.txt", str, 128); - if (!ret) - { - printf("Page fetched successfully - read %d characters\n", strlen(str)); - printf("Result: %s\n", str); - } - else - { - printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); - } - - //POST data - HTTPMap map; - HTTPText inText(str, 512); - map.put("Hello", "World"); - map.put("test", "1234"); - printf("\nTrying to post data...\n"); - ret = http.post("http://httpbin.org/post", map, &inText); - if (!ret) - { - printf("Executed POST successfully - read %d characters\n", strlen(str)); - printf("Result: %s\n", str); - } - else - { - printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); - } - - //PUT data - strcpy(str, "This is a PUT test!"); - HTTPText outText(str); - //HTTPText inText(str, 512); - printf("\nTrying to put resource...\n"); - ret = http.put("http://httpbin.org/put", outText, &inText); - if (!ret) - { - printf("Executed PUT successfully - read %d characters\n", strlen(str)); - printf("Result: %s\n", str); - } - else - { - printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); - } - - //DELETE data - //HTTPText inText(str, 512); - printf("\nTrying to delete resource...\n"); - ret = http.del("http://httpbin.org/delete", &inText); - if (!ret) - { - printf("Executed DELETE successfully - read %d characters\n", strlen(str)); - printf("Result: %s\n", str); - } - else - { - printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); - } + printf("Target: %s\r\n", URL); + printf("Likes: %d\r\n", facebookLikes(URL)); eth.disconnect(); +} - while(1) { + +int to_hex (int code) { + static char hex[] = "0123456789abcdef"; + return hex[code & 15]; +} + +/* urlencode code from + * Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) + */ +int urlencode (char *str, char *buf, int len) { +// char *pstr = str, *buf = (char*)malloc(strlen(str) * 3 + 1), *pbuf = buf; + char *pstr = str, *pbuf = buf; + + if (len < (strlen(str) * 3 + 1)) return -1; + while (*pstr) { + if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') + *pbuf++ = *pstr; + else if (*pstr == ' ') + *pbuf++ = '+'; + else + *pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15); + pstr++; } + *pbuf = '\0'; + return 0; }