Could not write file with PHP cURL with curlopt_stderr and curlopt_file

I'm trying to write information from a cURL hit to a log file, but I can’t do it. I run windows using wamp and completely control all users on the machine, and the log file and php that calls cURL are in the same directory for this test. I used the code below:

$session = curl_init();
$logfh = fopen("my_log.log", 'a+');
if ($logfh) {
  print "Opened the log file without errors";
}
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// Optional settings for debugging
curl_setopt($session, CURLOPT_VERBOSE, true);
curl_setopt($session, CURLOPT_FILE, $logfh); // logs curl messages
// curl_setopt($session, CURLOPT_STDERR, $logfh); // logs curl messages
curl_exec($session);
curl_close($session);

The log file opens without errors, and my cURL returns success, but nothing is written to the file. I used CURLOPT_FILEit CURLOPT_STDERRas an alternative, but it didn’t help the reason and I’m not sure that something is wrong with me. Any debugging suggestions would be appreciated.

+5
source share
3 answers

. URL-, , URL-, WILL .

, , :

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

, , fclose(), , .

+5

fclose() " ", , ... , :

  • CURLOPT_RETURNTRANSFER curl curl_exec().

  • CURLOPT_FILE CURLOPT_STDERR curl, ( ) .

, . , , :

CURLOPT_RETURNTRANSFER, :

$session = curl_init();
$logfh = fopen("my_log.log", 'a+');
if ($logfh !== false) {
  print "Opened the log file without errors";
}
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_VERBOSE, true);
$result = curl_exec($session);
curl_close($session);
fwrite($logfh, $result);
fclose($logfh);

CURLOPT_FILE, :

$session = curl_init();
$logfh = fopen("my_log.log", 'a+');
if ($logfh !== false) {
  print "Opened the log file without errors";
}
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_FILE, $logfh);
curl_setopt($session, CURLOPT_VERBOSE, true);
curl_exec($session);
curl_close($session);
fclose($logfh);
+4

Using CURLINFO_HEADER_OUT= true may cause a problem. I think there is something in CURLINFO_VERBOSEthat uses this option and just suppresses VERBOSE information ...

curl_setopt($ch, CURLINFO_HEADER_OUT, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_STDERR, fopen('log.log', 'a+'));

Just to mention, you can use:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_STDERR, fopen('log.log', 'a+'));

If you want to log the output to a file and still be able to return curl_exec.

0
source

All Articles