When you GET or POST this:
"http://translate.google.com/translate_a/t","client=t&sl=auto&tl=".urlencode($lang)."&text=".urlencode($text)
the response looks like this:
[[["автомобил","car","avtomobil",""]],,"en",,[["автомобил",[5],1,0,1000,0,1,0]],[["car",5,[["автомобил",1000,1,0],["автомобилот",0,1,0],["автомобили",0,1,0],["кола",0,1,0],["возило",0,1,0]],[[0,3]],"car"]],,,[["en","fr"]],30]
How to decode that? (maybe in json_decode style)
To get the api to return JSON change the parameter
client=t
to
client=p
//this will translate from en to ar -- you can change it for your needs
function translate_word($en){
$file_content = file_get_contents('http://translate.google.com/translate_a/t?client=p&sl=auto&tl=ar&hl=en&sc=2&ie=UTF-8&oe=UTF-8&uptl=ar&oc=1&prev=conf&psl=auto&ptl=en&otf=1&it=sel.8936&ssel=0&tsel=3&q='.str_replace(" ","%20",$en));
$obj = json_decode($file_content);
if(!empty($obj->sentences[0]->trans)){
return $obj->sentences[0]->trans;
}else{
return $en;
}
}
//how to use
echo translate_word("test translation process");
//if you do not know short codes for your language you can just open
translate.google.com and do described on screenshot
//you should have firebug extension installed on your browser
//if the answer was usefull please do not forget to vote up! thanks.
Try using PHP's json_decode function to convert that data to native PHP data types.
Related
I have the following Json file:
[{"Datum":"21-07-2017","Zeit":"21:48","Menge":"545465476"},{"Datum":"21-07-2017","Zeit":"21:51","Menge":"78"},{"Datum":"21-07-2017","Zeit":"21:53","Menge":"456"}]
The Json validator says its ok.
But still I cannot this Json file via php_decode(). Instead I am getting "bool(false)"
My code
$jso = json_encode($jfile);
var_dump(json_decode($jso));
$error = json_last_error();
What am I doing wrong?
This script is saving an input to a txt database in Json format.
Your JSON encode and decode look correct. You can refer to the following resources for confirmation:
http://php.net/manual/en/function.json-decode.php
http://php.net/manual/en/function.json-encode.php
So the problem, most likely, lies in the way you get the json into the functions. In your case this is done with a variable "$jfile".
I suggest you do a var_dump of the $jfile variable to see what data it holds, if any.
My first guess, as others have pointed out as well, is that you're reading the json from a file (hence $jfile). This is usually done with the "file_get_contents" function which is described in detail here.
A working example
Your directory structure should look like this:
.
├── readjson.php
├── some_json_data.json
And your php code should look something like this:
<?php
$jfile = file_get_contents('some_json_data.json');
var_dump($jfile)
$jso = json_encode($jfile);
var_dump($jso);
$decoded_json = json_decode($jso);
var_dump($decoded_json);
$error = json_last_error();
?>
Note how I add more var_dump's and variables to explicitly monitor the state of the json objects. This might be helpful when learning new stuff.
I guess you have to first get the file into php, so $jso will hold json data (in terms of file contents)
$jso = file_get_contents('JSON_FILE_PATH');
//then simply use json_decode
var_dump(json_decode($jso));
I'm writing a php api with methods wired together by php routing, such that
dev.myphp.com/user/login?email=sdfsdf#fdfd.com&password=d514
will return a json/xml response.
However, I do not know how to return this response, even if I use json_encode or xml_encode to conver the data string.
Do I just need to uniquely echo the JSON string out?
Always try to take data using POST method not GET , untill unless you don't find it less useful in caompare to POSt.
use simply
<?Php
--- php code
return json_encode($resultdata);
?>
it would be enough.
I have a search form in SPIP which does a GET to /recherche
Now when I am typing something like cinéma the é is automatically being encoded to something like cin%25C3%25A9ma
In SPIP, if I do
<?php echo urldecode($_GET['recherche']; ?>
It is being converted back to cinéma.
However, In SPIP, I would retrieve the value of recherche with something like this:
#ENV{recherche}
Is there someway to decode this variable. Any in-built function in SPIP?
You can create your own filter, adding this function in your mes_fonctions.php file:
function filtre_urldecode($v) {
return urldecode($v);
}
You then can use [(#ENV{recherche}|urldecode)] in your squelette.
There is no built-in function to decode an URL in SPIP. You have to use PHP's urldecode().
[(#ENV{recherche}|urldecode)] will do the job.
For reference, please see SPIP's filters: Adding your own filters
You don't have to create a new dedicated SPIP filter, since you can use PHP's urldecode as an SPIP filter. For example:
[(#ENV{recherche}|urldecode)]
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.
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).