Querying a json file from a url string (php) - 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.

Related

Is there a way to read an object's contents (file_get_contents) and get the object metadata using just one read in Google Cloud Storage?

Currently, I'm using PHP to fetch an object's custom metadata from Google Cloud Storage using something like this:
$meta_data_array = $storage->bucket('bucket_name')->object('objectname.html')->info()['metadata'];
And, then I fetch the contents using this:
$contents = file_get_contents('gs://bucket_name/objectname.html');
Is there a way to combine both into a single PHP database read like this?
$object = $storage->bucket('bucket_name')->object('objectname.html');
$meta_data_array = $object->info()['metadata'];
$contents = $object->downloadContents();
// ^^^ similar to downloadToFile($destination)
I haven't found anything in my doc searches. But it seems that something like this could exist and maybe I just don't know where to look. Please help.
You can use Google\Cloud\Storage\StorageObject::downloadAsString().

Take variables from a PHP script

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.

I want to send requests from a UE4 c++ game to my php script, so that it interacts with a mysql database

i'm searching the inet for around 3 days now and i'm stuck at this.
I got a MySQL Database and a php Script, as well as a Game made in UE4.
UE4 uses c++.
So now i want to send requests from the c++ game to the php script and that shall interact with the database.
For example create an account or login. I also want to pass the mysql query result of the php script to my c++ class.
I tried using HttpRequest, but i can't get data from php to c++ with that.
Maybe you can, but i don't understand it at all.
What i accomplished by now is that you can send a POST request from the game to the php script and pass variables so that the script uses them to perform the mysql query.
But how can i pass data from the php file to c++ now? The response i get is always the whole site (head and body) and i don't know where i could save the query result to pass it to the c++ code.
I'm a full beginner here, so go easy on me. I read so many different posts and blogs that my brain hurts like hell ): I hope someone can tell me how to do this easily or at least give me a hint on what i have to google and what i could use. I don't need a full tutorial, just a name of a library better than the Http.h (if simple HttpRequest cant manage this) would be enough. ): I'm really frustrated...
eXi
The PHP script should retun a HTTP response reduced to a bare minimum. It doesn't even need to be a HTML document:
<?php
// file: api.php
$param = $_POST['myparam'];
$foo = bar($param); // $foo contains e.g. "1,ab,C"
echo $foo; // if you opened http://myhost.com/api.php in a browser
// all you would see is "1,ab,C"
// (which is not a valid HTML document, but who cares)
?>
Then parse this HTTP response (a plain string, that is) from your game. You can use your own data format, or use a well-known format of your choice (XML or JSON are good candidates).
The json object in unreal is pretty good, so I would recommend outputting json from your php script. Json in php is a pretty natural workflow.
<?php
$obj['userid'] = 5476;
$obj['foo'] = 'bar';
echo json_encode($obj);
php?>
That will echo out
{"userid":5476,"foo":"bar"}
If that's all you output in your script then it's pretty straightforward to treat that as a string and populate an unreal json object with it.
FString TheStuffIGotFromTheServer;
TSharedPtr<FJsonObject> ParsedJson;
TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(TheStuffIGotFromTheServer);
if (FJsonSerializer::Deserialize(JsonReader, ParsedJson))
{
FString foo = ParsedJson.GetStringField("foo");
double UserId = ParsedJson.GetNumberField("userid");
}
Check out the unreal json docs to get a feel for what you can do with it.

How do I use external JSON...?

spent a few hours trying to figure this out, but cannot for the life of me figure out what's going wrong.
All I'm trying to do is load this:
https://recruit.zoho.com/ats/EmbedResult.hr?jodigest=2cV.Sr2As6VxhLMxQGuTNij*g.Fb3J7ysduDs.AC9sU-&atslocale=en_GB&rawdata=json
which I believe is json, into either javascript/jquery or php and use the data.
I've looked into jsonp, followed some tutorials, used some demos as templates and just can't get the above data to work.
If anyone can shed some light it would be much appreciated. It really shouldn't be this complicated, but I don't know what's going wrong.
Yep, that's JSON. The site may not support JSONP, so you're gonna have to use PHP to do this.
This is untested, but should work.
<?php
$url = 'https://recruit.zoho.com/ats/EmbedResult.hr?jodigest=2cV.Sr2As6VxhLMxQGuTNij*g.Fb3J7ysduDs.AC9sU-&atslocale=en_GB&rawdata=json';
$JSON = file_get_contents($url);
// echo the JSON (you can echo this to JavaScript to use it there)
echo $JSON;
// You can decode it to process it in PHP
$data = json_decode($JSON);
var_dump($data);
?>
JSONP relies on the server to return a JSONP formatted response. Basically, to use JSONP the server needs to return a JSON string wrapped in a function invocation ({"foo":1} becomes func({"foo":1})).
As the server your using doesn't return a JSONP response, you cannot use JSONP, you can only use JSON.
This is a shame, as JSON cannot be used x-domain due to the same origin policy (SOP). Therefore, the only option you have is to use a proxy server, which retrieves the JSON from the server, and either gives it to you in JSONP (see Yahoo Pipes), or which is on the same domain as the requested page (write a simple PHP script to get the file using file_get_contents() and then echo the output), in which case it can return the JSON.
I breifly looked at the requirements and it looks like you need an API key as well as an account. I saw that the site provides services for XML and JSON only. It looks to be fairly well documented.

using file get contents on a url that requires post data

i need to do a "file_get_contents" on a URL and there has to be data posted to the url before the contents are retrieved. is this possible? maybe something like
$contents = file_get_contents($_POST"http://www.example.com/");
so that i can then work with the $contents variable?
You cannot*** POST data using file_get_contents, you must use something like cURL
* I mark this because it is actually possible taking advantage of the third parameter which uses http context(see example one). However it really isn't worth your trouble if you have something like cURL.
Ah, I have tried to do this. Simply put you can't unless you install new extra software on your sever and go through A LOT of hassel and server load.
Best bet is to use GET if at all possible!
:)

Categories