Take variables from a PHP script - php

I am making a website to display the data at https://api.captcoin.com/address/top/100. I need to be able to make the website take variables("address", "percent", "balance", and "rank") from this script and make them local variables in my site so I can display them. How can I take these variables and use them in my site?

First you need to get the remote page contents:
$remote = file_get_contents('link');
Then, since the data is in json format, you need to decode it using json_decode function.
$data = json_decode($remote, true);
true means that $remote should be decoded as associative array.
And finally you can access the data like an ordinary php array:
echo $data['top'][0]['address'];
Also, you should add some logic to handle situations when remote server is not accessible.

Use json_decode to convert the content of that url into an array and then search through it like you would through any array.
To get the actual content of the site please refer to this post Get file content from a URL?

You can either do it with javascript or php.
With javascript use this:
http://api.jquery.com/jquery.getjson/
You take the pages output and push them as variables to the php.
With php you can use
http://php.net/manual/en/function.json-decode.php
You make an array to push the json data into:
$object = array();
$json = file_get_contents('https://api.captcoin.com/address/top/100');
$object = json_decode ( string $json , true)
Be aware this is untested and read the json_decode api to customize the function-call.

Related

Parsing Wikipedia API with PHP

I have a PHP script that retrieves the JSON result from the Wikipedia API and stores it in $json variable, then I json decode it into $data:
<?php
$q = htmlspecialchars(($_GET["q"]));
$url = 'https://en.wikipedia.org/w/api.php?action=query&list=search&srnamespace=0&srprop=snippet&format=json&callback=json&origin=*&prop=links|extracts|categories|images&srsearch=test';
$json = file_get_contents($url);
/*
print "<pre>";print_r($json);print "</pre>";
*/
$data = json_decode($json,true);
echo $data['query']['search'][0]['title'];
This retrieves the JSON file, but I am not able to work with it. I need to extract the Title tag and echoing it like this doesn't do anything.
echo $data['query']['search'][0]['title'];
Any idea how to correct my code to retrieve the following title tag:
Remove &callback=json from your URL. That's making a request for JSONP (ironic link to wikipedia). It wraps the response with a JavaScript callback function, so instead of just JSON you need in PHP, you're getting
/**/json(THE JSON HERE)
You can see it in the page source, even if it displays as JSON on the page. Those extra characters are making json_decode fail. That parameter is intended more for cross-domain requests from JS.
It looks like you're already accessing the resulting array properly with
echo $data['query']['search'][0]['title'];
You might think it would give you some kind of warning or notice when you try to access those array keys when $data is null, but it won't.

Querying a json file from a url string (php)

I'm trying to use the data.gov.uk to grab the court data and do cool stuff with it. Problem I am having is if I try to decode the version from the server in php: https://courttribunalfinder.service.gov.uk/api.html it just won't load. It only works if I download the courts.json file and decode from that. However then I'm unable to query the data with something like courts.json?q=bristol say.
So my question is, should I be able to decode directly from the gov website or do I need to do it locally, and if that's the case how would I allow me to feed query strings to my local json file?
e.g. I want to be able to do something like:
$jsonurl = directory() . '/courts.json?q=old+bailey';
$json = wp_remote_get($jsonurl,0,null,null);
$court = json_decode($json['body'], true);
well,
There are two options for you
1) Use cURL Library as mentioned by Joanvo
2) use YQL(Yahoo Query Language) that is also another powerful thing around.

How to send a POST parameter that will arrive in PHP as an associative array?

I want to send data to PHP API that reads data like this:
$id = $_POST['id'];
$data = $_POST['data'];
$name = $data['name'];
$foo = $data['sub']['foo'];
to send the ID, the query would look like id=123
but I'm not sure how the data parameter should be encoded - is it simply URL encoding of id=123&data['name']=name -> id=123&data%5B%27name%27%5D=name and similarly for the deeper nested array?
(I'm doing the request from Android, but I'm interested in the body of the POST request)
Yes you can definitely do that by making sure the parameter are being encoded properly
For example if you encode the following
ser[name]=Bob Smith&user[age]=47&user[sex]=M&
user[dob]=5/12/1956&pastimes[0]=golf&pastimes[1]=opera&
pastimes[2]=poker&pastimes[3]=rap&children[bobby][age]=12&
children[bobby][sex]=M&children[sally][age]=8&
children[sally][sex]=F&flags_0=CEO
Go to this site and paste either your code or the code that I provided
http://meyerweb.com/eric/tools/dencoder/ then click encode
and pass it to your URL as parameters.
If you need to generate those via PHP, maybe you should take a look at this
http://php.net/manual/en/function.http-build-query.php
Just use JSON. You send JSON from your app and do a simple json_decode in the Server.
In your case where you have to follow an API use this format:
data[name]=User&data[sub][foo]=Hithere
Source:
http://homakov.blogspot.de/2012/06/x-www-form-urlencoded-vs-json-pros-and.html?m=1

Passing variables through GET for a file_get_contents - a different way

I have information stored in a session. But was I just found out, sessions don't work when you do file_get_contents().
So after some searching I thought of using this technic:
$cart = base64_encode(serialize($_SESSION['cart']));
I pass my session cart, serialize it and encode it. I then pass it into the file_get_contents.
$url = "http://www.domain.com/pdf_order.php?cart=".$cart;
$html = file_get_contents($url);
In the URL that it gets, I have this:
$cart = unserialize(base64_decode($_GET['cart']));
But I don't get anything.
I can print out the GET cart and I have an encoded string, but then I can't do anything with it.
Any help, much appreciated.
UPDATE
When I echo the URL I get this
http://www.domain.com/pdf_order.php?cart=YTozOntzOjc6Imdhcm1lbnQiO2E6MTp7aTowO2E6Nzp7czo0OiJ0eXBlIjtzOjU6IlNoaXJ0IjtzOjY6ImFjY2VzcyI7czozOiJ5ZXMiO3M6MzoiaWREIjtpOjEwNDtzOjY6ImZhYnJpYyI7YToyOntzOjQ6InR5cGUiO2E6Mzp7czo4OiJkYl92YWx1ZSI7czo0OiJiZXN0IjtzOjExOiJwcmV0dHlfbmFtZSI7czoxNToiSSB3YW50IHRoZSBiZXN0IjtzOjM6InR4dCI7czoxNDY6IjEwMCUgQ290dG9uPGJyIC8+SWYgeW91IHdhbnQgdGhlIGJlc3QsIHdlJ2xsIGdpdmUgdGhlIGJlc3QuICBDb3R0b24gaXMgdGhlIG1vc3QgY29tZm9ydGFibGUgZmFicmljIGFuZCBpcyBhIGZhYnJpYyB0aGF0IGJyZWF0aHMuLi55b3UnbGwgZW5qb3kgaXQuIjt9czo1OiJwcmljZSI7aTo5MDt9czozOiJpZFMiO3M6MjoiNTIiO3M6OToicXR5X1NoaXJ0IjtzOjE6IjQiO3M6NDoicXR5XyI7czoxOiI0Ijt9fXM6MzoiaWRBIjtzOjg6IjExMTExMTUxIjtzOjExOiJhY2Nlc3NvcmllcyI7YTo0OntpOjA7YTo0OntzOjM6ImlkQSI7czoxOiIxIjtzOjQ6Im5hbWUiO3M6ODoiU2xpbSB0aWUiO3M6MzoicXR5IjtzOjE6IjQiO3M6NToicHJpY2UiO3M6NToiMjcuOTkiO31pOjE7YTo0OntzOjM6ImlkQSI7czoxOiIyIjtzOjQ6Im5hbWUiO3M6ODoiU2xpbSB0aWUiO3M6MzoicXR5IjtpOjE7czo1OiJwcmljZSI7czo1OiIyNy45OSI7fWk6MjthOjQ6e3M6MzoiaWRBIjtzOjE6IjMiO3M6NDoibmFtZSI7czo4OiJTbGltIHRpZSI7czozOiJxdHkiO2k6MTtzOjU6InByaWNlIjtzOjU6IjI3Ljk5Ijt9aTozO2E6NDp7czozOiJpZEEiO3M6MToiNCI7czo0OiJuYW1lIjtzOjg6IlNsaW0gdGllIjtzOjM6InF0eSI7aToxO3M6NToicHJpY2UiO3M6NToiMjcuOTkiO319fQ==
Check out How to post data in PHP using file_get_contents? it's pretty much doing what you want to do, by creating a stream.
I'm guessing you can still put $_SESSION variables in there too.
If you are working on same server as www.domain.com then why do you need to use file_get_contents() to get some data or do some processing . Use direct calls.

virustotal.com API example in PHP?

Does anybody have a PHP example of using the VirusTotal.com public API for URL scanning?
The API is available here:
http://www.virustotal.com/advanced.html
There's a Python/JSON example, but I'm not experienced with either :(
All you need is to retreive the report json using file_get_contents this way :
$json = file_get_contents('https://www.virustotal.com/api/get_url_report.json');
Then use json_decode to convert your json into a php array:
$array = json_decode($json);
to see results :
var_dump($array);
to post data use curl, related question.
A PHP example is available on that page (maybe it's new).

Categories