curl - PHP - Can curl_exec( ) return partial results? -


i'm using php's built in curl functions make requests , capture headers (for specific purpose of displaying headers). works well. have curl_setopt($ch,curlopt_followlocation,true); set, redirects followed , @ end, there groups of headers each http request made, including redirects.

the problem redirect 'stale' , point domain no longer exists. in case, curl_exec( ) throws error , returns no data.

however in specific use here, useful curl_exec( ) return "whatever it's got far" on error, @ least show headers pages worked, , able see goes wrong. can't find way accomplish this.

$ch = curl_init($url); curl_setopt($ch,curlopt_returntransfer,true); curl_setopt($ch,curlopt_ssl_verifypeer,false); curl_setopt($ch,curlopt_header,true); curl_setopt($ch,curlinfo_header_out,true); curl_setopt($ch,curlopt_followlocation,true); $result = curl_exec($ch); 

any thoughts?

maybe use curlopt_headerfunction headers?

$received_headers=array(); curl_setopt($ch,curlopt_headerfunction,function($ch,string $header) use(&$received_headers):int{     $received_headers[]=$header;     return strlen($header); }); curl_exec($ch); var_dump($received_headers); 

that said, in situation, happier debug data provided curlopt_verbose - doesn't give headers, if there's connection drop or wierd that, tell you, have idea of happened other headers received. instead, recommend like:

$debug_str=''; $debugfileh=tmpfile(); $debugfile=stream_get_meta_data($debugfileh)['uri']; curl_setopt_array($ch,array(     curlopt_verbose=>true,     curlopt_stderr=>$debugfileh )); curl_exec($ch); $debug_str=file_get_contents($debugfile); fclose($debugfileh); unset($debugfile,$debugfileh); var_dump($debug_str); 

note, solution above give headers, not response body recieved far (if any), that, you'd have combine both (curlopt_headerfunction or curlopt_verbose) , curlopt_writefunction

edit: fixed bugs in 2nd code, original code wrote give curl_setopt silent errors...


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -