Page 1 of 2
How do i use fwrite() correctly
Posted: Thu Aug 10, 2017 4:58 pm
by sjuaq
Hi,
I'm new to PHP and i trying to build one php script which consists in taking examples and adapt to my idea. The problem is that i'm having troubles figuring out what's wrong and how can i fix it :/
Here is a snip of the code:
Code: Select all
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://$user_ip:81");
$server_output = curl_exec ($ch);
curl_close ($ch);
$ourFileName = "$user_ip";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fwrite($ourFileHandle, $ch);
fclose($ourFileHandle);
?>
As you may understand i'm going directly to my problem and my problem is with fwrite().
I identify the file to right as $ourFileHandle and the text to write as $ch. Well, $ch is all the page fetch with Curl so the following error popup.
fwrite() expects parameter 2 to be string
How can i fix it and still save the result page?
Thanks
Re: How do i use fwrite() correctly
Posted: Thu Aug 10, 2017 9:44 pm
by requinix
If you're writing the entire contents of a file at once then use
file_put_contents instead.
To answer the question, the second argument has to be a string - as the error message says. $ch is not a string. It's a cURL resource. Look at your code and decide what variable you should be using instead.
Re: How do i use fwrite() correctly
Posted: Fri Aug 11, 2017 11:17 am
by sjuaq
I tried using file_put_contents instead of fwrite() but the problem remains it's not a string. All that i want is cURL to make the request and save the content to a file. In almost all cases is a html file or a empty file. How can i convert the cURL resource $ch to a string.
Re: How do i use fwrite() correctly
Posted: Fri Aug 11, 2017 11:51 am
by thinsoldier
an html file is a string
Re: How do i use fwrite() correctly
Posted: Fri Aug 11, 2017 11:54 am
by thinsoldier
fwrite($ourFileHandle, $ch);
try
fwrite($ourFileHandle, $server_output);
I suggest trying
http://docs.guzzlephp.org/en/stable/
Re: How do i use fwrite() correctly
Posted: Fri Aug 11, 2017 12:06 pm
by sjuaq
thinsoldier wrote:fwrite($ourFileHandle, $ch);
try
fwrite($ourFileHandle, $server_output);
That was the first thing i tried after i revisit this forum looking for answers.
It doesn't work, it only writes 1 to the file
Re: How do i use fwrite() correctly
Posted: Fri Aug 11, 2017 12:24 pm
by thinsoldier
> it only writes 1 to the file
Which means it probable worked successfully and returned true which can be in the form of 1. (0 if false)
Try CURLOPT_RETURNTRANSFER
http://php.net/manual/en/function.curl-exec.php
Returns TRUE on success or FALSE on failure. However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure.
Or just use Guzzle.
Re: How do i use fwrite() correctly
Posted: Fri Aug 11, 2017 12:40 pm
by sjuaq
thinsoldier wrote:> it only writes 1 to the file
Which means it probable worked successfully and returned true which can be in the form of 1. (0 if false)
Try CURLOPT_RETURNTRANSFER
http://php.net/manual/en/function.curl-exec.php
Returns TRUE on success or FALSE on failure. However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure.
Or just use Guzzle.
I have tried almost all the methods on php.net, it's all working great in the cURL part. The problem is
fwrite() expects parameter 2 to be string, resource given in
$ch is not a string. It's a cURL resource as stated by
requinix
About Guzzle, NO why would i need another library for a small project?
Re: How do i use fwrite() correctly
Posted: Fri Aug 11, 2017 9:16 pm
by requinix
Oh. I missed it because I assumed you already had it in place.
You need the CURLOPT_RETURNTRANSFER option set. Otherwise cURL will output the HTML rather than return it from curl_exec().
Re: How do i use fwrite() correctly
Posted: Sat Aug 12, 2017 7:48 am
by sjuaq
I have set CURLOPT_RETURNTRANSFER to 1.
But lets go directly to the problem, is there any way to retrieve the cURL request info into a valid string. After making the request convert $ch to a string using let's say encode/decode method and use it as a valid way to fwrite() to a file. I have tested the fwrite() using plaintext or any other value and it works (it outputs to the file). So the main problem here is the convertion of the text retrieve by cURL and the valid string for fwrite().
This should be a piece of cake to a more experienced programmer. :/
Re: How do i use fwrite() correctly
Posted: Sat Aug 12, 2017 8:57 am
by requinix
sjuaq wrote:is there any way to retrieve the cURL request info into a valid string.
The request headers, response headers, and response body are all potentially available to you as strings. So far it only looks like you want the response body. You get that as a string by using CURLOPT_RETURNTRANSFER, which you say you've set but if you had then you wouldn't have a problem anymore. So post your current code.
sjuaq wrote:After making the request convert $ch to a string using let's say encode/decode method and use it as a valid way to fwrite() to a file.
No. You cannot convert $ch to a string.
Re: How do i use fwrite() correctly
Posted: Sat Aug 12, 2017 12:48 pm
by sjuaq
requinix wrote:
The request headers, response headers, and response body are all potentially available to you as strings. So far it only looks like you want the response body. You get that as a string by using CURLOPT_RETURNTRANSFER, which you say you've set but if you had then you wouldn't have a problem anymore. So post your current code.
Here is the current code, i'm not including the whole page to make it a small thread.
I added 2 curl_setopt lines and the result remains the same. You said that i can have more responses (headers) in the file, what should i do to get that info too... (Nevertheless if it doesn't save to a file is useless)
Code: Select all
...
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://$user_ip:81");
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$server_output = curl_exec ($ch);
curl_close ($ch);
$ourFileName = "$user_ip";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fwrite($ourFileHandle, $ch);
fclose($ourFileHandle);
?>
Re: How do i use fwrite() correctly
Posted: Sat Aug 12, 2017 1:41 pm
by requinix
You. Cannot. Use. $ch. As. A. String.
There's one other significant variable available to you. Try using that instead.
Re: How do i use fwrite() correctly
Posted: Sat Aug 12, 2017 4:36 pm
by sjuaq
Which one are you talking?
Re: How do i use fwrite() correctly
Posted: Sat Aug 12, 2017 10:43 pm
by requinix
Look at your code. Do you know what it does? Do you understand how it works? What the variables and functions are? What they mean? What they represent?
You know what CURLOPT_RETURNTRANSFER does, right? Hopefully you looked at the docs, or else you figured it out based on what's been said here. So now the question is, that behavior it enables, where do you get the results? Your code is already set up to make use it of, but all you're missing is for it to click in your head regarding what to do next.
It's right there. You are two seconds away from having the right code if you could just open your eyes.