How can I get all meaning of a word in json response .Preferably without key/signing up/api key .If I type test then all meaning of that word.
I tried :
but its not working.
<?php
$query = 'oracle';
$file = file_get_contents('http://www.google.com/dictionary/json?callback=a&q='.$query.'&sl=en&tl=en&restrict=pr,de&client=te');
// var_dump($file);
$file = substr($file, 2, -10);
$file = preg_replace("/\\\x[0-9a-f]{2}/", "", $file);
echo $file;
$json = json_decode($file);
var_dump($json);
?>
Even this is returning null.
I have tried Only the php above.I would like to knowif I can make rest call without api key just words which match the query word .Is there any rest call you have in mind.I really appreciate any help .Thanks in Advance.
Assuming server side
Get a copy of a dictionary in a computer friendly format i.e. http://www.ibiblio.org/webster/ (XML).
Store said dictionary in a database or in memory and perform a lookup.
Would then be trivial to provide a restful service returning all definitions for a particular word.
Also see: Google's "define: " through an API?
Related
I'm relatively new to Perl and trying to self teach. However i have read all of the related threads on this page and others and none of them seem to work for me.
Below is my code - trying to get a lot of data from a webpage in Perl format and export it to update values in an SQL table.
Currently i can't even data dumper the results of the url out.
Any help would be great.
#!/usr/bin/perl
#
use LWP::Simple;
use warnings;
use strict;
use JSON qw( decode_json from_json );
use LWP::Simple;
use Data::Dumper;
use utf8;
my $url = "http://.sensitivedata.txt";
my #json= from_json(get ( $url ));
die "Couldn't get $url" if not defined #json;
##my $decoded_json = decode_json( #json);
print Dumper #json;
exit 0;
This is the error message it is giving me:
defined(#array) is deprecated at alarms.pl line 14.
(Maybe you should just omit the defined()?)
malformed JSON string, neither array, object, number, string or atom, at character offset 0 (before "(end of string)") at /opt/csw/share/perl/csw/JSON.pm line 168
The error message is pretty clear about a) what the problem is and b) how to get rid of it.
defined(#array) is deprecated at alarms.pl line 14. (Maybe you should
just omit the defined()?)
Calling defined() on #json is pointless. You're really just checking to see if there is any data in the array so replace if not defined #json with if not #json.
That will get rid of the error message. But you'll still have a problem as your program will almost certainly now die on the same line with the error message "Couldn't get http://.sensitivedata.txt". And that's probably not an accurate error message.
The problem is that this error can be caused by two problems. Either you can't get the data or you can't parse the data. Your error message only mentions one of these possibilities. Better to split the error checking into two.
# Step 1: Get the data
my $raw_json = get($url);
die "Can't get data from $url" unless $raw_json;
# Step 2: Parse the data
my #json = from_json($raw_json);
if (!#json) {
warn $raw_json;
die "Can't parse data from $url";
}
With code more like this, you'll be able to see what the problem is.
There's another little problem here, so to pre-empt your next question...
from_json always returns a scalar. It will either be a hash reference or an array reference (depending on the JSON you get). Looks like you're expecting an array. You'll need to store the reference in a scalar and dereference it.
my $json_array_ref = from_json($raw_json);
if (!#$json_array_ref) {
warn $raw_json;
die "Can't parse data from $url";
}
my #json = #$json_array_ref;
I have written a perl script which accessed JIRA REST API to GET a list of issues that match a specific JQL query. Sometimes thee results are only one issue and other times I get many back.
$client->GET(
$apiPath.$jql.$fieldRes,
$headers);
#a perl hash of results
my $response = from_json($client->responseContent());
while $response is a perl hash, if I try to drill down into the hash I hit an issue.
There is an array of "issues" within the hash.
I am trying to pull data with "foreach" for each specific issue but I keep getting errors:
foreach my $issues ($response->{'issues'})
{
print STDERR Dumper($issues->{'key'});
}
Error...
Pseudo-hashes are deprecated at script.pl line #.
Argument "JIRA-10011" isn't numeric in hash element at script.pl line #.
Bad index while coercing array into hash at script.pl line #.
Any help is appreciated
There is an array of "issues" within the hash.
You cannot put arrays into hashes in Perl, this is only possible for array references. So you need to dereference it when iterating the hash(ref) with your foreach.
foreach my $issues ( #{ $response->{'issues'} } )
{
print STDERR Dumper($issues->{'key'});
}
Since you will get one issue per iteration, you should rename $issues to $issue so you won't get confused later.
I'm trying to get & store in a text file all the all addresses from the "address" column in the next page:
http://bitcoinrichlist.com/top100
It should be very simple, but I never worked with something like this before, I always received data from mysql databases or php variables, but never from a html page.
Someone may explain me how should I do this?
Storing the addresses it's easy, fwrite command right to a text file on my server.
But what about receiving the data? How should I do it?
edit:
My question in short is: How should I save all the addresses from the "address" column in a text file?
You can use the PHP Simple HTML DOM Parser to pull in a web page and then traverse its DOM. Example from their quick start guide:
// Create DOM from URL or file
$html = file_get_html('http://www.google.com/');
// Find all images
foreach($html->find('img') as $element)
echo $element->src . '<br>';
// Find all links
foreach($html->find('a') as $element)
echo $element->href . '<br>';
Once you gather up the elements, loop through them and write out to a file.
I am trying to insert image in database. The database has a field named images and its type is BLOB. I am trying to insert the image, but only the first 2.2KB is stored. Even if I insert another image it stores only 2.2KB in the database.
When I try to show this image in my application it doesn't show; it's just a small icon, not the image. How can I insert the image in the right way?
use CGI;
my $file = $q->param("file");
$file = 'C:/wamp/bin/apache/apache2.2.22/cgi-bin/images/2.jpg';
open(my $fh, $file);
my $data;
binmode($fh);
read($fh, $data, (stat($fh))[7]);
close($fh);
my $Data = {
table =>'student',
data => {
Image => $fh,
}
};
Data::Insert($Data);
print $q->header;
print $q->start_html(
-title => "student",
);
print $q->end_html;
showImage.pl
my $q = new CGI();
my $handle = Dbm::connection();
$id = $q->param('id_person');
$getimage = $handle->selectrow_array (<<SQLEOF);
SELECT Image
FROM student
WHERE ID = '$id'
SQLEOF
print "Content-Type: image/jpeg\n";
print "Content-length: \n\n";
binmode STDOUT;
print STDOUT $getimage;
My recomendation is keep the image as base 64 encrypted to the db with the MIME type of the image. When you need it, just decrypt it by saying MIME type. This is the mostly used to upload files using ajax. So why can't we use the same way to store image directly to DB?
Just give an additional column to keep MIME type in your table and take it along with the encoded data as print it together.
From a file extension, we can identify the type of file. The MIME type for images are mainly
image/gif: GIF image
image/jpeg: JPEG JFIF image;
image/pjpeg: JPEG JFIF image;
image/png: Portable Network Graphics;
image/svg+xml: SVG vector image;
image/tiff: Tag Image File Format (only for Baseline TIFF);
You can create a new column by giving name as mime_typ . Now when you enctrypt a file using base 64 encryption, keep it as a string like we store usernames and passwords in a table. Similarly add the MIME type to the mime_typ column. when you want to show the image, print the encrypted content after decoding it along with the content in the MIME type, which is stored in the same row in the mime_typ column. You can search google for the way to show an image which is encrypted in base 64 encryption.
You need to read the file in binary mode - i.e.
open(my $fh, $myfile);
my $data;
binmode($fh);
read($fh, $data, (stat($fh))[7]);
close($fh);
I'm not sure why you deleted the database-insert code from the question, but I found it in the revision history.
The issue could be because you aren't not using bind variables, and the binary image contains an escape character which is causing a problem.
I recommend using DBIx::Simple to help create your insert statements that will help create the bind variables for you. DBIx::Simple works with both SQL::Abstract and SQL::Interp. I find SQL::Interp more flexible.
It also appears to be a bug that you are inserting the file handle into the image field instead of the file data. Try adding use File::Slurp (which you may need to install), and then putting this in your %data hash:
Image => scalar read_file($file, { binmode => ':raw' });
Your SELECT statement is also vulnerable to a SQL injection attack because you did not validate the outside input before passing it to the database, and you did not you bind variables again. Using DBIx::Simple, the same code would look like this:
my $db = DBIx::Simple->new($handle);
$getimage = $db->iquery("SELECT Image FROM student WHERE ID = ",\$id)->list;
Also, I recommend omitting the Content-Length header, or properly calculating it, rather than leaving it present in an invalid state.
I want to store text/string in a text field in a database.
This string has the variable $name in it.
When I pull it out of the database, I want that variable to be substituted with the value I define before I print the string.
# Variable I want to substitute #
1. $name='John';
# needs to be read from database #
2. $txt{'hello'}="Hello ${name}, How are you?";
3. print "<tag>$txt{'hello'}</tag>";
It prints Hello John, How are you? as required, but when 2nd line is read from database, it displays Hello ${name}, How are you?.
Some things I found are:
Locale::Maketext
$string =~ s/(\$\w+)/$1/eeg;
my $string = sprintf 'Say hello to %s and %s', $foo, $bar;
Can someone guide me about how to go about it?
What you're describing is a template. There are lots of template systems on CPAN of varying degrees of complexity. Text::Template and Template Toolkit are a couple of popular ones. You don't want to let your templates access arbitrary variables; that's a security hole. Instead, put the variables they're allowed to access into a hash.
If all you need is a very simple system, you can do something like this:
sub fill_in_template
{
my ($text, $values) = #_;
$text =~ s/ \$\{ ( [^}\s]+ ) \} /$values->{$1}/gx;
return $text;
}
my %txt;
my %values = (name => 'Your Name');
my $template = 'Hello ${name}, How are you?'; # $name NOT interpolated
$txt{'hello'} = fill_in_template($template, \%values);
print "<tag>$txt{'hello'}</tag>\n";
You might add some error checking in case the template uses a field that's not defined. But if you need something more complicated than that, you're probably better off picking an existing template system from CPAN.
Locale::Maketext is intended for internationalization (so your app can produce output in multiple languages without your translators needing to work on the code directly) and is not the sort of template engine you're looking for.