PHP using curl to obtain remote image implementation method _javascript skills

Source: Internet
Author: User
Tags curl http cookie http post

Curl requires PHP to support the environment, you can run the Phpinfo () function is supported, the general to be php.ini; Extension=php_curl.dll before, remove, restart IIS or Apache.
The code is as follows:


/* *@ Curl to get the specified picture to the local
*@ complete picture address
*@ The filename to be
stored
/function getimg ($url = "", $filename = "")
{ c7/>//remove possible quotes above URL connections
 //$url = Preg_replace ('/(?: ^[' "]+|[') /]+$)/', ', $url);
 $hander = Curl_init ();
 $fp = fopen ($filename, ' WB ');
 curl_setopt ($hander, Curlopt_url, $url);
 curl_setopt ($hander, Curlopt_file, $fp);
 curl_setopt ($hander, curlopt_header,0);
 curl_setopt ($hander, curlopt_followlocation,1);
 curl_setopt ($hander, curlopt_returntransfer,false);//returns data as data flow, when false is shown directly
 curl_setopt ($hander, CURLOPT_TIMEOUT,60);
 Curl_exec ($hander);
 Curl_close ($hander);
 Fclose ($FP);
 return true;
}

When called, Direct getimg ("/logo.jpg", "upload/image.jpg")

Implementation Code 2
The code is as follows:

<?php $url = "Picture absolute address/thumbnail.jpg";
$filename = ' curl.jpg ';
Getimg ($url, $filename); /* *@ Curl Way to get the picture to the local *@ complete picture address *@ the filename to be stored/function getimg ($url = "", $filename = "") {if Is_dir (basename ($fil
  ename)) {echo "The Dir is not exits";
 return false; //Remove the possible quotes above the URL connection $url = Preg_replace ('/(?: ^[']+|[')
 /]+$)/', ', $url);
 $hander = Curl_init ();
 $fp = fopen ($filename, ' WB ');
 curl_setopt ($hander, Curlopt_url, $url);
 curl_setopt ($hander, Curlopt_file, $fp);
 curl_setopt ($hander, curlopt_header,0);
 curl_setopt ($hander, curlopt_followlocation,1); curl_setopt ($hander, curlopt_returntransfer,false);//returns data in the form of a data stream, when false is shown directly curl_setopt ($hander, Curlopt_
 TIMEOUT,60); /* $options = Array (curlopt_url=> '/thum-f3ccdd27d2000e3f9255a7e3e2c4880020110622095243.jpg ', Curlopt_file => $
 FP, Curlopt_header => 0, curlopt_followlocation => 1, curlopt_timeout => 60);
 Curl_setopt_array ($hander, $options);
 * * CURL_EXEC ($hander);
 Curl_close ($hander);Fclose ($FP);
return true;
 }?>

curl_setopt set an option for the curl call
BOOL curl_setopt (INT-ch, string option, mixed value)
The curl_setopt () function sets the option for a curl session. The option parameter is the setting you want, and value is the values given by this choice.
the values of the following options will be used as long reshaping (specified in the option argument):
curlopt_infilesize: When you upload a file to a remote site, this option tells PHP the size of the file you uploaded.
Curlopt_verbose:If you want to curl report every unexpected thing, set this option to a value other than 0.
Curlopt_header:If you want to include a header in the output, set this option to a value other than 0.
curlopt_noprogress:If you don't have PHP to display a process bar for curl transmission, set this option to a non 0 value.
Note:PHP automatically sets this option to a value other than 0, and you should only change this option for debugging purposes.
Curlopt_nobody:If you do not want to include the body part in the output, set this option to a value other than 0.
Curlopt_failonerror: If you want PHP to be in error (HTTP code returns greater than or equal to 300), do not display, set this option to a person not 0 value. The default behavior is to return a normal page, ignoring the code.
Curlopt_upload:If you want PHP to be ready for upload, set this option to a value other than 0.
Curlopt_post: If you want PHP to do a regular HTTP POST, set this option to a non 0 value. This post is an ordinary application/x-www-from-urlencoded type, most of which is used by HTML forms.
curlopt_ftplistonly: Set this option to a value other than 0, PHP lists the list of directory names for FTP.
Curlopt_ftpappend: Set this option to a value other than 0, PHP will apply the remote file instead of overwriting it.
CURLOPT_NETRC: Set this option to a value other than 0, PHP will find the username and password of the remote site you want to connect to in your ~./netrc file.
curlopt_followlocation: Set this option to a non 0 value (like "Location:"), the server will send it as a part of the HTTP header (note that this is recursive, PHP will send the shape as "Location:" header).
Curlopt_put:Set this option to a non 0 value to upload a file with HTTP. To upload this file you must set Curlopt_infile andcurlopt_infilesizeOptions.
Curlopt_mute:Set this option to a value other than 0, PHP will be completely silent for the curl function.
curlopt_timeout:Set a long integer number as the maximum number of seconds to continue.
Curlopt_low_speed_limit:Set a long shaping number to control how many bytes are transferred.
Curlopt_low_speed_time:Set a long shaping number to control how many seconds to transfer curlopt_low_speed_limit The specified number of bytes.
Curlopt_resume_from:Pass a long shaping parameter that contains the byte offset address (the start form you want to transfer to).
curlopt_sslversion: Passes a long parameter that contains an SSL version. The default PHP will be determined by its own efforts, and in more security you must set it manually.
Curlopt_timeconditiOn: Passes a long parameter specifying how the Curlopt_timevalue parameter is handled. You can set this parameter to Timecond_ifmodsince or timecond_isunmodsince. This is for HTTP only.
Curlopt_timevalue:Passes a number of seconds from 1970-1-1 onwards to the present. This time is used by the Curlopt_timevalue option as the specified value or by default timecond_ifmodsince.
the values of the following options will be used as strings:
Curlopt_url:This is the URL you want to retrieve with PHP. You can also set this option when initializing with the Curl_init () function.
curlopt_userpwd: Passing a string in the form of a [Username]:[password] style, which functions as PHP to connect.
curlopt_proxyuserpwd: Passes a string in the form of [Username]:[password] to connect to the HTTP proxy.
Curlopt_range: Pass a range that you want to specify. It should be "XY" format, X or Y is excepted. HTTP transmissions also support several intervals, separated by a x-y,n-m.
Curlopt_postfields:A string that passes all data as an HTTP "POST" operation.
Curlopt_referer: Contains a string of "referer" headers in the HTTP request.
curlopt_useragent: Contains a string of "user-agent" headers in the HTTP request.
Curlopt_ftpport: Passes an IP address that contains the FTP "POST" instruction. This post instruction tells the remote server to connect to the IP address we specified. This string can be an IP address, a host name, a network interface name (under Unix), or '-' (using the system default IP address).
Curlopt_cookie:Passes a header connection that contains an HTTP cookie.
Curlopt_sslcert: Passes a string containing the PEM format certificate.
curlopt_sslcertpasswd: Passes a password that is required to use the Curlopt_sslcert certificate.
Curlopt_cookiefile: A string that passes the name of a file that contains cookie data. This cookie file can be in Netscape format, or in the HTTP style header that is stockpiled in the file.
curlopt_customrequest:When an HTTP request is made, passing a character is used by the get or head.

The above is PHP using curl to obtain remote image implementation method, hope to help everyone's learning.

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.