Curl: Submit a POST request to a website. How can I obtain the returned file name?

Source: Internet
Author: User
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-DispositionGet 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_contentWrite open file handle

curl -O -d "key=val" urlIn 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.