I'd love to start programming in JSON, for riot api, but I don't know how to start it.. I have done something like that, but this doesn't show anything lol.. Just white page.
<html>
<head>
<title>JSON example</title>
<script language="javascript" >
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://euw.api.pvp.net/api/lol/euw/v2.5/league/by-summoner/31827832?api_key=myapikey');
// Set so curl_exec returns the result instead of outputting it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Get the response and close the channel.
$response = curl_exec($ch);
curl_close($ch);
$json = json_decode($response, true);
foreach($json as $elem){
echo $elem[0]['name'];
echo $elem[0]['tier'];
}
</script>
</head>
<body>
</body>
</html>
Please take a look at these guides I wrote for an introduction to the Riot API and using/understanding JSON.
While you can use many languages like PHP, I teach it in Javascript/Ajax/JQuery as that knowledge than be applied to other languages pretty easily, especially with PHP since the syntax of both look decently similar.
[Tutorial] Beginners introduction to Riot API and JSON, using Javascript and Ajax
I discuss what the API is and how you use it, as well as securing your key. I also mention JSON and how to access and understand it with a program.
Let me know if you have any questions.
Related
I am trying to get one specific css class from my DOM object. I use simplehtmldom library.
1) The library
simplehtmldom.sourceforge.net
2) Because my localhost doesnt support fopen for some reason, I use the CURL library to get the HTML, source:
http://simplehtmldom.sourceforge.net/manual_faq.htm
3) Now, my script looks like this. It gives me source of HTML from the website which I desire.
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://hokejbal.cz/1-liga/tabulky/");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
print $result;
str_get_dom;
$ret = $html->find('.standings tablesort tablesorter tablesorter-default');
?>
4) Now, I want to get just a part of the website. Exactly this table:
<table class="standings tablesort tablesorter tablesorter-default">
I found it in Google Chrome webmaster tools
Unfortunately, when I run the script, I get whole HTML page, not just the desired part. What am I doing wrong?
The selector would be '.standings.tablesort.tablesorter.tablesorter-default'
Update: Try the below code.
<?php
$html = file_get_html('http://hokejbal.cz/1-liga/tabulky/');
$ret = $html->find('table.standings', 0);
print $ret;
?>
I just wanted to know if anyone knows how I can integrate a travel/deals API on my college project website.
I have looked at the Rome2Rio API and signed up and have the API key but I have no idea what i do next.Or if you think that is a hard one to use could you suggest an alternative easy one to integrate.
Any help would be great. I have added a link below to the documentation that might help you.
http://www.rome2rio.com/documentation/search
$url = 'http://<server>/api/1.4/json/Search?key=<key>&oName=Bern&dName=Zurich&noRideshare';
$content = file_get_contents($url);
$json = json_decode($content, true);
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($json));
foreach($iterator as $key => $value) {
echo "<p>$key => $value</p>";
}
I'm trying to use oEmbed to test if a Youtube or Vimeo video exists. My code works fine with Youtube, but it doesn't work with Vimeo, even if I follow Vimeo's official documentation for oEmbed and the example provided there. Why is my code not working for Vimeo? I get a server response 200 for Youtube, but a server response 0 for Vimeo. My code is:
<?php
//Problematic case: I get 0 as a server reponse
$cURL = curl_init("https://vimeo.com/api/oembed.json?url=https%3A//vimeo.com/76979871");
// Youtube case - works fine: server response = 200
// $cURL = curl_init("http://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=ebXbLfLACGM");
// Set option 1: return the result as a string
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
// Set option 2: Follow any redirect
curl_setopt($cURL, CURLOPT_FOLLOWLOCATION, true);
// Execute the query
$cURLresult = curl_exec($cURL);
// Get the HTTP response code
$response = curl_getinfo($cURL, CURLINFO_HTTP_CODE);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Server response</title>
</head>
<body>
<h1><?php print "Server response: " . $response; ?></h1>
</body>
</html>
The problem came from using localhost to connect to Vimeo. Localhost works fine with Youtube, but not with Vimeo. I've test the above script in a real server, as suggested by another member, and there everything works fine.
Moral of the history: always test problematic external connections in a real server, not on localhost.
In my application, I am trying to display some data from google.com.
My problem is that I always have this error :
Error #2048: Security sandbox violation: https://XXXXX.com/runtime.swf cannot load data from http://www.google.com/finance/company_news?q=null:OZG&output=rss.
The crossdomain.xml of google looks like that:
<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<site-control permitted-cross-domain-policies="by-content-type" />
</cross-domain-policy>
And I call the page in my AS3 code like that :
var hdr:URLRequestHeader = new URLRequestHeader("Content-type", "text/x-cross-domain-policy");
var request:URLRequest = new URLRequest(feedURL);
request.requestHeaders.push(hdr);
//then load the request with an UrlLoader
Do you have any idea of why flash give me this cross damin security error?
I actually misunderstood the meaning of the crossdomain.xml file. Basically, the crossdomain policy file of google doesn't allow any domain.
I think that my only solution is to use a proxy...
You can use a PHP proxy, but cURL must be enabled.
For example:
$gogle = curl_init();
curl_setopt($gogle , CURLOPT_URL, $googleurl);
curl_exec($gogle);
If you need further manipulations use the CURLOPT_RETURNTRANSFER option to return the content as a string instead of outputting it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER , TRUE);
$content = curl_exec($ch);
use CURLOPT_POSTFIELDS if you need to pass POST agouments (eg: "param=value&q=google")
I am wondering how facebook has implemented the search functinality on the home page. as soon as i type 'a' the dropdown comes with the list of friends and its very very fast..
I saw in firebug that it sends a ajax request to one of its file.
I wanted to implement the same functionality in one of my webapp but even though my table has just 4 records it takes bit time to load the dropdown.
What i have done is
send ajax req with my search parameter
executed sql query
made the html
and returned it so it will
replace the div
Facebook has very expensive servers using a very expensive CDN (Akamai) and uses server-side caching like memcached.
If you can predict with reasonable accuracy the things the user might search for (e.g. a known friends and friends-of-friends list) and pre-cache them on the server you can do this quickly. If you deliver that list with the webpage in the first place and cache it on the client, it will be lightning fast (once the page is loaded anyway).
Try the following PHP code, it will crawl into the Fast Facebook Search site and echo the results. I hope it will be helpful, feel free to tweak it :)
<?php
function facebook_search_api($args, $referer = 'YOUR SITE ADDRESS', $endpoint = 'web')
{
$url = "http://www.FastFacebookSearch.com".$endpoint;
if ( !array_key_exists('v', $args) )
$args['v'] = '1.0';
//$args['key']="ABQIAAAArMTuM-CBxyWL0PYBLc7SuhT2yXp_ZAY8_ufC3CFXhHIE1NvwkxT-uD75NXlWUsDRBw-8aVAlQ29oCg";
//$args['userip']=$_SERVER['REMOTE_ADDR'];
$args['rsz']='8';
$url .= '?'.http_build_query($args, '', '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $referer);
$body = curl_exec($ch);
curl_close($ch);
//decode and return the response
return json_decode($body,true);
}
$query_temp=urldecode(isset($_GET['q'])?$_GET['q']:"none");
$search_type=urldecode(isset($_GET['search_engine'])?$_GET['search_engine']:"");
echo "$search_type Search Results for: $query_temp<br />-----<br />";
$query=$search_type.$query_temp;
$res = google_search_api(array('q' => $query));
$pages=$res['responseData']['cursor']['pages'];
$nres=0;
for($i=0;$i<count($pages);$i++)
{
$res = google_search_api(array('q' => $query,'start'=>$rez['responseData']['cursor']['pages'][$i]['start']));
for($j=0;$j<count($res['responseData']['results']); $j++)
{
$nres++;
echo urldecode("<a href=".$res['responseData']['results'][$j]['url'])."><big>";
echo urldecode($res['responseData']['results'][$j]['title'])."</a></big><br />";
echo urldecode("<font color=green><small>".$res['responseData']['results'][$j]['url'])."</small></font><br>";
echo urldecode("<iiisearch>".$res['responseData']['results'][$j]['content'])."<br><br>";
}
}
echo "<br />---<br />Total number of reuslts: $nres";
?>