PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Thu Oct 19, 2017 1:19 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: Thu Aug 10, 2017 4:58 pm 
Offline
Forum Newbie

Joined: Thu Aug 10, 2017 4:48 pm
Posts: 17
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:
Syntax: [ Download ] [ Hide ]
$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


Top
 Profile  
 
PostPosted: Thu Aug 10, 2017 9:44 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6573
Location: WA, USA
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.


Top
 Profile  
 
PostPosted: Fri Aug 11, 2017 11:17 am 
Offline
Forum Newbie

Joined: Thu Aug 10, 2017 4:48 pm
Posts: 17
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.


Top
 Profile  
 
PostPosted: Fri Aug 11, 2017 11:51 am 
Offline
Forum Contributor

Joined: Fri Jul 20, 2007 11:29 am
Posts: 338
an html file is a string

_________________
Warning: I have no idea what I'm talking about.


Top
 Profile  
 
PostPosted: Fri Aug 11, 2017 11:54 am 
Offline
Forum Contributor

Joined: Fri Jul 20, 2007 11:29 am
Posts: 338
fwrite($ourFileHandle, $ch);

try

fwrite($ourFileHandle, $server_output);

I suggest trying http://docs.guzzlephp.org/en/stable/

_________________
Warning: I have no idea what I'm talking about.


Top
 Profile  
 
PostPosted: Fri Aug 11, 2017 12:06 pm 
Offline
Forum Newbie

Joined: Thu Aug 10, 2017 4:48 pm
Posts: 17
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


Top
 Profile  
 
PostPosted: Fri Aug 11, 2017 12:24 pm 
Offline
Forum Contributor

Joined: Fri Jul 20, 2007 11:29 am
Posts: 338
> 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.

_________________
Warning: I have no idea what I'm talking about.


Top
 Profile  
 
PostPosted: Fri Aug 11, 2017 12:40 pm 
Offline
Forum Newbie

Joined: Thu Aug 10, 2017 4:48 pm
Posts: 17
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?


Top
 Profile  
 
PostPosted: Fri Aug 11, 2017 9:16 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6573
Location: WA, USA
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().


Top
 Profile  
 
PostPosted: Sat Aug 12, 2017 7:48 am 
Offline
Forum Newbie

Joined: Thu Aug 10, 2017 4:48 pm
Posts: 17
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. :/


Top
 Profile  
 
PostPosted: Sat Aug 12, 2017 8:57 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6573
Location: WA, USA
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.


Top
 Profile  
 
PostPosted: Sat Aug 12, 2017 12:48 pm 
Offline
Forum Newbie

Joined: Thu Aug 10, 2017 4:48 pm
Posts: 17
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)

Syntax: [ Download ] [ Hide ]
...
$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);
?>
 


Top
 Profile  
 
PostPosted: Sat Aug 12, 2017 1:41 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6573
Location: WA, USA
You. Cannot. Use. $ch. As. A. String.

There's one other significant variable available to you. Try using that instead.


Top
 Profile  
 
PostPosted: Sat Aug 12, 2017 4:36 pm 
Offline
Forum Newbie

Joined: Thu Aug 10, 2017 4:48 pm
Posts: 17
Which one are you talking?


Top
 Profile  
 
PostPosted: Sat Aug 12, 2017 10:43 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6573
Location: WA, USA
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours


Who is online

Users browsing this forum: Bing [Bot], Exabot [Bot] and 17 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group