Curl submits a POST request to a URL URL to return a file if the output will output the contents of this file how to get the returned file name or how to save the file?
function Liansuo_post ($url, $data) {//Analog commit data function $curl = Curl_init ();//Start a Curl session curl_setopt ($curl, Curlo Pt_url, $url); Address to access curl_setopt ($curl, Curlopt_ssl_verifypeer, 0); Inspection of the source of the certification Certificate curl_setopt ($curl, Curlopt_ssl_verifyhost, 1); Check the SSL encryption algorithm for the presence of curl_setopt ($curl, curlopt_useragent, $_server[' http_user_agent ') from the certificate; Simulates user-used browser curl_setopt ($curl, curlopt_followlocation, 1); Use automatic jump curl_setopt ($curl, Curlopt_autoreferer, 1); Auto set Referer curl_setopt ($curl, Curlopt_post, 1); Send a regular POST request curl_setopt ($curl, Curlopt_postfields, $data); Post-Submitted packet curl_setopt ($curl, Curlopt_cookiefile, $GLOBALS [' cookie_file ']); Read the cookie information stored above curl_setopt ($curl, Curlopt_timeout, 30); Set timeout limit to prevent dead loops curl_setopt ($curl, Curlopt_header, 0); Displays the contents of the Returned header area curl_setopt ($curl, Curlopt_returntransfer, 1); The information obtained is returned as a file stream $tmpInfo = curl_exec ($curl); Perform the action
$rinfo =curl_getinfo ($curl);
if (Curl_errno ($curl)) {
Echo ' Errno '. Curl_error ($curl);
}
Curl_close ($curl); Critical Curl Session
Print_r ($rinfo);
return $tmpInfo; Return data
}
Reply content:
Curl submits a POST request to a URL URL to return a file if the output will output the contents of this file how to get the returned file name or how to save the file?
function Liansuo_post ($url, $data) {//Analog commit data function $curl = Curl_init ();//Start a Curl session curl_setopt ($curl, Curlo Pt_url, $url); Address to access curl_setopt ($curl, Curlopt_ssl_verifypeer, 0); Inspection of the source of the certification Certificate curl_setopt ($curl, Curlopt_ssl_verifyhost, 1); Check the SSL encryption algorithm for the presence of curl_setopt ($curl, curlopt_useragent, $_server[' http_user_agent ') from the certificate; Simulates user-used browser curl_setopt ($curl, curlopt_followlocation, 1); Use automatic jump curl_setopt ($curl, Curlopt_autoreferer, 1); Auto set Referer curl_setopt ($curl, Curlopt_post, 1); Send a regular POST request curl_setopt ($curl, Curlopt_postfields, $data); Post-Submitted packet curl_setopt ($curl, Curlopt_cookiefile, $GLOBALS [' cookie_file ']); Read the cookie information stored above curl_setopt ($curl, Curlopt_timeout, 30); Set timeout limit to prevent dead loops curl_setopt ($curl, Curlopt_header, 0); Displays the contents of the Returned header area curl_setopt ($curl, Curlopt_returntransfer, 1); The information obtained is returned as a file stream $tmpInfo = curl_exec ($curl); Perform the action
$rinfo =curl_getinfo ($curl);
if (Curl_errno ($curl)) {
Echo ' Errno '. Curl_error ($curl);
}
Curl_close ($curl); Critical Curl Session
Print_r ($rinfo);
return $tmpInfo; Return data
}
When sending curl requests, add
//获取响应的消息头curl_setopt($curl, CURLOPT_HEADER, 1);curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);
Parse HTTP header to Content-Disposition get file name
$tmpInfo = curl_exec($curl);list($headers, $body) = explode("\r\n\r\n", $tmpInfo, 2);$header_array = explode("\n", $headers[0]);foreach($header_array as $header_value) { $header_pieces = explode(':', $header_value); if(count($header_pieces) == 2) { $headers[$header_pieces[0]] = trim($header_pieces[1]); }}$file_name = $headers['Content-Disposition'];$file_type = $headers['Content-Type'];$file_content = $body;
You can refer to the above code, if not Content-Disposition , you need special treatment, you can see where the other person returns the file name.
Save file only need to $file_content write open file handle
curl -O -d "key=val" urlThe returned content is saved as a file of the same name.
Are you going to return the "file name" or the file content? If you want the file name, check the "content-disposition" header first, without using the filename on the URL. If you want the contents of the file, write a write function and pass it to curl.