When Curl submits a POST request to a website address, it will return a file. If the output will output the content of this file, how can we get the returned file name or how can we save this file? {code ...} $ rinfocurl_getinfo ($ curl); if (curl_errno ($ curl) {echo & #039; Errn... when Curl submits a POST request to a website address, it will return a file. If the output will output the content of this file, how can we get the returned file name or save the file?
Function liansuo_post ($ url, $ data) {// simulate the data submission function $ curl = curl_init (); // start a CURL session curl_setopt ($ curl, CURLOPT_URL, $ url ); // curl_setopt ($ curl, CURLOPT_SSL_VERIFYPEER, 0); // check the certificate source curl_setopt ($ curl, CURLOPT_SSL_VERIFYHOST, 1 ); // check from the certificate whether the SSL encryption algorithm has curl_setopt ($ curl, CURLOPT_USERAGENT, $ _ SERVER ['HTTP _ USER_AGENT ']); // simulate the user's browser curl_setopt ($ curl, CURLOPT_FOLLOWLOCATION, 1); // use automatic jump curl_setopt ($ curl, CURLOPT_AUTOREFERER, 1 ); // automatically set Referer curl_setopt ($ curl, CURLOPT_POST, 1); // send a regular Post request curl_setopt ($ curl, CURLOPT_POSTFIELDS, $ data ); // Post the submitted data packet curl_setopt ($ curl, CURLOPT_COOKIEFILE, $ GLOBALS ['cookie _ file']); // read the cookie information stored above curl_setopt ($ curl, CURLOPT_TIMEOUT, 30); // set the timeout limit to prevent endless loops curl_setopt ($ curl, CURLOPT_HEADER, 0); // display the returned Header area content curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1 ); // The obtained information is returned as a file stream $ tmpInfo = curl_exec ($ curl); // perform the operation
// $ Rinfo = curl_getinfo ($ curl );
If (curl_errno ($ curl )){
Echo 'errno'. curl_error ($ curl );
}
Curl_close ($ curl); // key CURL session
// Print_r ($ rinfo );
Return $ tmpInfo; // return data
}
Reply content:
When Curl submits a POST request to a website address, it will return a file. If the output will output the content of this file, how can we get the returned file name or save the file?
Function liansuo_post ($ url, $ data) {// simulate the data submission function $ curl = curl_init (); // start a CURL session curl_setopt ($ curl, CURLOPT_URL, $ url ); // curl_setopt ($ curl, CURLOPT_SSL_VERIFYPEER, 0); // check the certificate source curl_setopt ($ curl, CURLOPT_SSL_VERIFYHOST, 1 ); // check from the certificate whether the SSL encryption algorithm has curl_setopt ($ curl, CURLOPT_USERAGENT, $ _ SERVER ['HTTP _ USER_AGENT ']); // simulate the user's browser curl_setopt ($ curl, CURLOPT_FOLLOWLOCATION, 1); // use automatic jump curl_setopt ($ curl, CURLOPT_AUTOREFERER, 1 ); // automatically set Referer curl_setopt ($ curl, CURLOPT_POST, 1); // send a regular Post request curl_setopt ($ curl, CURLOPT_POSTFIELDS, $ data ); // Post the submitted data packet curl_setopt ($ curl, CURLOPT_COOKIEFILE, $ GLOBALS ['cookie _ file']); // read the cookie information stored above curl_setopt ($ curl, CURLOPT_TIMEOUT, 30); // set the timeout limit to prevent endless loops curl_setopt ($ curl, CURLOPT_HEADER, 0); // display the returned Header area content curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1 ); // The obtained information is returned as a file stream $ tmpInfo = curl_exec ($ curl); // perform the operation
// $ Rinfo = curl_getinfo ($ curl );
If (curl_errno ($ curl )){
Echo 'errno'. curl_error ($ curl );
}
Curl_close ($ curl); // key CURL session
// Print_r ($ rinfo );
Return $ tmpInfo; // return data
}
Add
// Obtain the Response Message Header curl_setopt ($ curl, CURLOPT_HEADER, 1); curl_setopt ($ curl, CURLOPT_BINARYTRANSFER, 1 );
Parse the Http HeaderContent-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;
Refer to the above Code. If noContent-Disposition
, You need special processing, you can see where the object name returned by the other party is.
To save the file, you only need$file_content
Write open file handle
curl -O -d "key=val" url
In this way, the returned content is saved as a file with the same name.
Are you going to return the "file name" or the file content? If you want a file name, first check the "Content-Disposition" header. If not, use the file name on the URL. If you want the file content, write a write function to the curl.