473,385 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

fread usage

a week ago 2 kind persons suggested me to use fread to read a result page.
i've been working for it but just can't get what i want.
$keyword ="big problem";
$startfig = rand(0,60);
//$theFile
="http://images.google.com/images?q=\"".$keyword."\"&start=".$startfig;
//$theFile
="http://images.google.com/images?q=\"".$keyword."\"&start=".strval($startfig );
$theFile ="http://images.google.com/images?q=acd";

if (($f = fopen($theFile, 'r')) === false) exit;

$contents = fread($f, (9*(1024*1024)));

echo "$contents";
fclose ($f);

$matchno = preg_match_all("/.*<img src=/images?q=tbn:.*:(.*)
width=.*>.*/", $contents, $regs);
$imgurl = $regs[rand(0,$matchno-1)];

why can't i retrieve the result obtained from google image search?
May 4 '06 #1
5 1528
"vito" <vi*********@yahoo.com.hk> writes:
a week ago 2 kind persons suggested me to use fread to read a result page.
i've been working for it but just can't get what i want.

why can't i retrieve the result obtained from google image search?


Dont you get any result at all in your $content var? no content at
all?

Also I would recommend you use file_get_contents() instead, it's alot
easier that fread and fopen.

--
Henrik Hansen
May 4 '06 #2
vito wrote:
a week ago 2 kind persons suggested me to use fread to read a result page.
i've been working for it but just can't get what i want.
$keyword ="big problem";
$startfig = rand(0,60);
//$theFile
="http://images.google.com/images?q=\"".$keyword."\"&start=".$startfig;
//$theFile
="http://images.google.com/images?q=\"".$keyword."\"&start=".strval($startfig );
$theFile ="http://images.google.com/images?q=acd";

if (($f = fopen($theFile, 'r')) === false) exit;

$contents = fread($f, (9*(1024*1024)));

echo "$contents";
fclose ($f);

$matchno = preg_match_all("/.*<img src=/images?q=tbn:.*:(.*)
width=.*>.*/", $contents, $regs);
$imgurl = $regs[rand(0,$matchno-1)];

why can't i retrieve the result obtained from google image search?

The url you feed to google does NOT bring me any results at all, instead
I get this error:
"The requested URL /?q=%22big%20problem%22&start=30 was not found on
this server."

Looks like google likes another syntax. "big problem" fed to the image
search page on my google yields the following url:

http://images.google.nl/images?svnum...22&btnG=Zoeken

Of course your result will be slightly different, you don't get the
Dutch google I assume. The quotes are never used AFAIK in google.

Maybe your problem lies here, not in the fread you use.
Sh.

--
Our POP server was kidnapped by a weasel.
May 4 '06 #3
vito wrote:
a week ago 2 kind persons suggested me to use fread to read a result page.
i've been working for it but just can't get what i want.
$keyword ="big problem";
$startfig = rand(0,60);
//$theFile
="http://images.google.com/images?q=\"".$keyword."\"&start=".$startfig;
//$theFile
="http://images.google.com/images?q=\"".$keyword."\"&start=".strval($startfig );
$theFile ="http://images.google.com/images?q=acd";

if (($f = fopen($theFile, 'r')) === false) exit;

$contents = fread($f, (9*(1024*1024)));

echo "$contents";
fclose ($f);

$matchno = preg_match_all("/.*<img src=/images?q=tbn:.*:(.*)
width=.*>.*/", $contents, $regs);
$imgurl = $regs[rand(0,$matchno-1)];

why can't i retrieve the result obtained from google image search?


OK, Vito, what happens when you try this?

One thing I see as a possible problem - you're reading up to 9MB. Do you have
that much memory available to PHP? You might have run out. Check your error logs.

BTW - do you have Google's permission? Last I heard they didn't like people
doing this.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
May 4 '06 #4
after reading your comments,

i made some changes.

1)switch to use "acd" instead of "big problem"
2)try use file_get_contents instead
$theFile ="http://images.google.com/images?q=acd";
the $content contains:

<img
src=/images?q=tbn:8w7TF3ADocTy3M:cheminf.cmbi.ru.nl/cheminf/isis/pict/acd_price.gif
width=134 height=105>

but i just don't know why

$matchno = preg_match_all("/.*<img src=/images?q=tbn:.*:(.*) width=.*>.*/",
$content, $regs);

returns nothing. ($matchno echo nothing)

furthermore, how is Jerry able to count the size? could anybody tell me?
indeed, i need not retrieve the whole content but just a single little image
is grateful.

May 4 '06 #5
vito wrote:
after reading your comments,

i made some changes.

1)switch to use "acd" instead of "big problem"
2)try use file_get_contents instead
$theFile ="http://images.google.com/images?q=acd";
the $content contains:

<img
src=/images?q=tbn:8w7TF3ADocTy3M:cheminf.cmbi.ru.nl/cheminf/isis/pict/acd_price.gif
width=134 height=105>

but i just don't know why

$matchno = preg_match_all("/.*<img src=/images?q=tbn:.*:(.*) width=.*>.*/",
$content, $regs);

returns nothing. ($matchno echo nothing)

furthermore, how is Jerry able to count the size? could anybody tell me?
indeed, i need not retrieve the whole content but just a single little image
is grateful.


Vito,

I was looking at your fread() call - you say get up to 9*1024*1024 bytes - which
is 9 mb.

And you are getting the correct information back. When you fetch a page
containing images, the webserver returns <img> tags. The browser must then go
out and fetch each of those images.

Open the page with your query and then look at the HTML source. This is what
your program should be getting. Writing that to the user's browser should cause
the browser to initiate further connections to retrieve the data.

As for your regex - ? is a control character (makes the preceding character
optional). You need to escape it, i.e.

... images\?q=tbn ...

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
May 4 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Luc Holland | last post by:
Hey, I'm working on a program that reads a binary file. It's opened with ==== if ((f1=fopen(argv,"rb"))==NULL) { fprintf(stderr,"Error opening %s for reading . . .\n",argv); exit(2); } ====...
10
by: Alain Lafon | last post by:
Helas, I got something that should be a minor problem, but anyhow it isn't to me right now. A little code fragment: fread(&file_qn, x, 1, fp_q); The corresponding text file looks like...
6
by: Patrice Kadionik | last post by:
Hi all, I want to make a brief comparison between read() and fread() (under a Linux OS). 1. Family read and Co: open, close, read, write, ioctl... 2. Family fread and Co: fopen, fclose,...
2
by: joel.washburn | last post by:
I'm looking for some information on why fread calls max out CPU usage on SSL connections in windows. I've tried upgrading PHP (now at 5.1.2) and OpenSSL (now 9.8) but cannot stop fread() from...
2
by: Richard Hsu | last post by:
// code #include "stdio.h" int status(FILE * f) { printf("ftell:%d, feof:%s\n", ftell(f), feof(f) != 0 ? "true" : "false"); } int case1() { FILE * f = fopen("c:\\blah", "wb+"); int i = 5;
13
by: 010 010 | last post by:
I found this very odd and maybe someone can explain it to me. I was using fread to scan through a binary file and pull bytes out. In the middle of a while loop, for no reason that i could...
5
by: David Mathog | last post by:
When reading a binary input stream with fread() one can read N bytes in two ways : count=fread(buffer,1,N,fin); /* N bytes at a time */ or count=fread(buffer,N,1,fin); /* 1 buffer at a...
5
by: howa | last post by:
are there any advantage in replacing all fread() operations with file_get_contents() ? i.e. file_get_contents("/usr/local/something.txt") VS $filename = "/usr/local/something.txt";
20
by: ericunfuk | last post by:
If fseek() always clears EOF, is there a way for me to fread() from an offset of a file and still be able to detect EOF?i.e. withouting using fseek(). I also need to seek to an offset in the file...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.