Recently because of the need to call the network disk interface to upload files, I used the CURL library, of course, before using CURL library must be enabled in PHP CURL settings
You can get curl information by using the Php_info () function, and if you don't see curl information, you need to set up PHP and open the library. Under Windows platform, you need to change the settings of the php.ini file, find the Php_curl.dll, and cancel the preceding semicolon annotation.
General file upload is through the HTML form, through the curl can not go through the browser, directly in the server-side simulation of the form submitted to complete post data, file upload and other functions.
We can get the content of the webpage by other means. Most of the time, because I want to be lazy, I directly use simple PHP functions, as follows:
$content = file_get_contents ("http://www.doucube.com");
Or
$lines = File ("http://www.doucube.com");
Or
ReadFile (http://www.doucube.com);
However, this approach lacks flexibility and effective error handling. Also, you can't use it to do some difficult tasks-such as processing coockies, validation, form submission, file uploads, and so on. So choose Curl Library.
Sample code:
<?php
$url = ' https://www.google.com ';
$method = ' POST ';
Headers and data (this is API dependent, some uses XML):
That is, headers and $data are used when the interface is invoked
$headers = Array (
' Accept:application/json ',
' Content-type:application/json ',
);
$data = Json_encode (Array (
' FirstName ' => ' John ',
' LastName ' => ' Doe '
)); Start a Curl session
$handle = Curl_init ();
curl_setopt ($handle, Curlopt_url, $url); The address to access
curl_setopt ($handle, curlopt_header,1); Whether to display the contents of the header area returned
curl_setopt ($handle, Curlopt_httpheader, $headers); Set Request headers
curl_setopt ($handle, Curlopt_returntransfer, true); Gets the information returned in the form of a file stream
curl_setopt ($handle, Curlopt_ssl_verifyhost, false); Check to see if the SSL encryption algorithm exists from the certificate
curl_setopt ($handle, Curlopt_ssl_verifypeer, false); Inspection of the source of certification certificate
Switch ($method) {
Case ' get ':
Break
Case ' POST ':
curl_setopt ($handle, Curlopt_post, true);
curl_setopt ($handle, Curlopt_postfields, $data); Set up the request body and submit the packet
Break
Case ' put ':
curl_setopt ($handle, curlopt_customrequest, ' put ');
curl_setopt ($handle, Curlopt_postfields, $data); Set up the request body and submit the packet
Break
Case ' DELETE ':
curl_setopt ($handle, Curlopt_customrequest, ' DELETE ');
Break
}
$response = curl_exec ($handle); Perform an action
$code = Curl_getinfo ($handle, Curlinfo_http_code); Gets the returned status code
Curl_close ($handle); Close Curl Session
if (' = = $code ') {
echo "OK";
}
Here is an example of an interest to see:
Upload files with curl is very convenient, what header,post strings are not generated, with Fsockopen to write a bunch of curl: ==============
PHP Code
$file = Array ("upimg" => "@e:/png.png");/file path, preceded by @, indicates file upload.
$curl = Curl_init ("http://localhost/a.php");
curl_setopt ($curl, curlopt_post,true);
curl_setopt ($curl, Curlopt_postfields, $file);
Curl_exec ($curl);
Fsockopen: ===============
PHP Code $uploadFile = file_get_contents ("E:/png.png"); $boundary = MD5 (time ());
$postStr. = "--". $boundary. " \ r \ n The boundary begins, note that the default is two more than the header definition boundary '-'
$postStr. = "Content-disposition:form-data; Name=\ "upimg\"; Filename=\ "e:/png.png\" "\ r \ n";
$postStr. = "content-type:image/png\r\n\r\n";
$postStr. = $uploadFile. " \ r \ n "; $postStr. = "--". $boundary. " \ r \ n ";/end of boundary
Write ($fp, "post/a.php http/1.0\r\n");
Fwrite ($fp, "Content-type:multipart/form-data boundary=". $boundary. " \ r \ n ");
Fwrite ($FP, "Content-length:". strlen ($POSTSTR). " \r\n\r\n ");
Fwrite ($fp, $POSTSTR);
while (!feof ($FP))
{
Echo fgets ($FP, 128);
}
Fclose ($FP);
a.php ==============
PHP Code Print_r ($_files);