I'm using CURL in my php script and I want to write information from CURLOPT_VERBOSE into file. I tried CURLOPT_STDERR but without luck. It still print everything in cmd window. I'm not sure if parameter in curl_setopt($ch, CURLOPT_STDERR, $errFileHandle); should be file handler or file name (both not work).
Running on a Unix based OS? Use the below on the command line to run your script. Using Windows OS, use the command on Cygwin.
php yourPhp.php > yourFile.txt
Or you can just run it in verbose mode with a file handle
<?PHP
$verbosPath = __DIR__.DIRECTORY_SEPARATOR.'verboseOut.txt';
echo "Saving verbose output to: $verbosPath\n";
$handle=curl_init('http://www.google.com/');
curl_setopt($handle, CURLOPT_VERBOSE, true);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_STDERR,$f = fopen($verbosPath, "w+"));
curl_exec($handle);
fclose($f);
?>
Related
I want to upload the file from local to server using curl in php and without using the form (which is html).
My php version is 5.2.6.
I have tried many different way and make many research about (upload file to server using curl in php), but the solution in google cannot solve my problem.
Below is my code:
// open file descriptor
$fp = fopen ("user.com/user/fromLocal.txt", 'w+') or die('Unable to write a file');
// file to download
$ch = curl_init('C:/wamp/www/user/fromLocal.txt');
// enable SSL if needed
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// output to file descriptor
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
// set large timeout to allow curl to run for a longer time
curl_setopt($ch, CURLOPT_TIMEOUT, 1000);
curl_setopt($ch, CURLOPT_USERAGENT, 'any');
// Enable debug output
curl_setopt($ch, CURLOPT_VERBOSE, true);
echo $ch;
echo $fp;
curl_exec($ch);
curl_close($ch);
fclose($fp);
Expected output:
The file can upload to server and view.
Actual output:
Unable to write the file
I think you miss important information.
fopen ("user.com/user/fromLocal.txt", 'w+')
this means nothing.
To send a file to the server the server has to be configured to accept a POST request and you have to send the file through that endpoint.
If you have a form, do you send it to: "user.com/user/fromLocal.txt" ??
You have to create the form data with curl and send it to a server ready to accept your request. There are different ways to accomplish that. And the most simple is exactly to send a form using curl and not the HTML. But absolutly you cannot write a file like that in a server.
I am trying to delete an xml file, and then re-create it with data from an xml feed using cURL.
my PHP code is as follows:
function get_file($file, $local_path, $newfilename){
//Delete old xml file first
unlink('../../xml/file.xml');
// Create a new one
$out = fopen($local_path.$newfilename,"wb");
if ($out == FALSE){
print "<br>File not opened<br>";
exit;
}
$ch = curl_init();
// Setup options, where to write etc
curl_setopt($ch, CURLOPT_FILE, $out);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 28800);
curl_setopt($ch, CURLOPT_URL, $file);
// Execute
curl_exec($ch);
// Close
curl_close($ch);
}
// get_file(url_of_file, directory_to_put, filename);
get_file('XML FEED URL', '../../xml/', 'file.xml');
this code used to work fine until I moved to an Ubuntu virtual server.
I want to use the code by running the following command:
php ../var/www/html/php/cron/download_xml_feed.php
the xml folder (and current xml file) are located at ../var/www/html/xml
Every time I run the command however it gives me:
PHP Warning: unlink(../../xml/jobs.xml): No such file or directory in /var/www/html/php/cron/download_xml_feed.php on line 7
PHP Warning: fopen(../../xml/jobs.xml): failed to open stream: No such file or directory in /var/www/html/php/cron/download_xml_feed.php on line 10
this will eventually be a crontab run with the above command
How can I get it to work?
I am using the following code to get some info from a website and save it locally.
$ch = curl_init("http://test.com/test.txt");
$fp = fopen("test.txt", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
Now the test.txt needs to be updated periodically. How can I trigger this at specific time intervals?
If you're on *nix or a Mac (or even Cygwin), you're probably better off using wget:
#hourly wget http://test.com/test.txt
That will do everything that cURL call will do, and do it on an hourly basis. Here's a good cron intro: https://help.ubuntu.com/community/CronHowto
I'm having some trouble with a particular cURL command and I'd like to see the headers and responses. In the command line I use -v and it displays everything, however...
In PHP I'm attempting to use:
curl_setopt($curl, CURLOPT_VERBOSE, 1);
However, nothing is being displayed.
I'm using PHP 5.3.24 on Windows Server 2008 on IIS.
Supposedly the info is sent into the stderr stream which I assume means the regular log used for PHP errors - however nothing is going there either. I'm getting no header results for cURL commands that I know are working and those that I know are not working.
My guess is that you need to also return the buffer. This code works under Linux and might work for you under Win2008:
$browser = curl_init();
curl_setopt($browser, CURLOPT_URL, $url);
curl_setopt($browser, CURLOPT_RETURNTRANSFER, true);
curl_setopt($browser, CURLOPT_VERBOSE, true);
$data = curl_exec($browser);
echo $data;
I'm trying something with curl in php.
I read about the use of CURLOPT_VERBOSE to help debugging.
But it gives no output. I checked my phpinfo() and under cURL stands:
debug => no
I guess that is the problem here. How can I set this to yes?
I looked in php.ini and could find it.
Also no luck on google.
also no luck here: How to enable features for php-curl
I hope someone can help me!
Having a debug build of cURL isn't required for the CURLOPT_VERBOSE setting to work, but by default CURLOPT_VERBOSE information gets output to STDERR which is only visible on the console. So if you are running PHP from a browser, the verbose output isn't sent anywhere you can see.
You can set the option CURLOPT_STDERR to a file handle and the verbose output should be written there.
You should be able to do this:
$stdout = fopen('php://output', 'w+');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com');
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_STDERR, $stdout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
I'm not sure if I'm encountering a PHP bug at the moment (running PHP 5.4.5 via FastCGI) because I don't see the verbose output in the browser, but if I run it from the command line I do. If I fwrite to $stdout I do see that output in the browser but still nothing from cURL so I know the handle is valid.
If you experience the same issue, here is a workaround:
$tempout = fopen('php://temp', 'w+');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com');
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_STDERR, $tempout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
rewind($tempout);
$debug = stream_get_contents($tempout);
echo $debug; // the data from CURLOPT_VERBOSE
Hope that helps.