PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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 :/
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.
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.
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.
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.
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?
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. :/
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.
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)
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.