3

I'm writing a script in which an unspecified number of files need to be uploaded via cURL requests to a remote API. However, this script hangs and eventually times out. Strangely enough, all the requests are successful (the files are successfully uploaded), but the script is unable to continue. Here's the loop:

foreach ($paths as $path) {
  $ch = curl_init($path);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-Auth-Token: '.$token, 'Content-Length: '.filesize($path));
  curl_setopt($ch, CURLOPT_PUT, true);
  curl_setopt($ch, CURLOPT_INFILE, fopen($path, 'r'));
  curl_setopt($ch, CURLOPT_INFILESIZE, filesize($path));
  echo curl_exec($ch);
}

I believe this has something to do with the loop. I've tried adding curl_close within the loop, but it doesn't solve the problem. Any ideas?

2
  • all the requests are successful and but the script is unable to continue together is likely impossible (The only chance would be that the scripts encounters a problem after successful upload of the last image) Commented May 4, 2013 at 6:27
  • I know it's strange, but the files appear in the target directory. I'm not sure what to say... Commented May 4, 2013 at 6:33

2 Answers 2

4

put timeout in CURL

foreach ($paths as $path) {
  $ch = curl_init($path);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-Auth-Token: '.$token, 'Content-Length: '.filesize($path));
  curl_setopt($ch, CURLOPT_PUT, true);
  curl_setopt($ch, CURLOPT_INFILE, fopen($path, 'r'));
  curl_setopt($ch, CURLOPT_INFILESIZE, filesize($path));
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,0); 
  curl_setopt($ch, CURLOPT_TIMEOUT, 400); //timeout in seconds
  echo curl_exec($ch);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! By adding the timeout I was finally able to see some errors, which I easily resolved. I was then able to remove the timeout and everything works great! I just wasn't sure the best way to troubleshoot this – the timeout was the trick.
-1

seperate curl from loop with calling a function like below

foreach ($paths as $path) {
call_curl($path);
}


function call_curl($path){
  $ch = curl_init($path);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-Auth-Token: '.$token, 'Content-Length: '.filesize($path));
  curl_setopt($ch, CURLOPT_PUT, true);
  curl_setopt($ch, CURLOPT_INFILE, fopen($path, 'r'));
  curl_setopt($ch, CURLOPT_INFILESIZE, filesize($path));
  echo curl_exec($ch);
}

2 Comments

Thanks. I'm trying this now, but I'm curious; why would this make a difference?
@David This doesn't make any difference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.