Recently used to upload files via post, the online rumor has curl post submission and fsockopen, which curl the simplest, so from the simplest.
This is simply to post a variable to another page
$url = ';
$data = Array (' A ' => ' B ');
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_post, true);
curl_setopt ($ch, Curlopt_postfields, $data);
$ret = curl_exec ($ch);
Curl_close ($ch);
Mainly said this option Curlopt_returntransfer: If set to TRUE/1, then curl_exec will not automatically output the content of the request page to the screen, $ret for the content of the request page, if set to false/0, then Curl_ exec automatically prints the content of the request page to the screen, and if the request succeeds, the content of the $ret is 1 or true.
The following is the code to upload local files, if you need to upload a remote file, the first down to the local, and then delete it (if you have any other way to inform the students):
$url = ';
$file = ' 1.jpg ';
$field [' uploadfile '] = ' @ '. $file; (UploadFile is the name of the receiving end)
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_post, 1);
curl_setopt ($ch, Curlopt_postfields, $field);
$ret = curl_exec ($ch);
Curl_close ($ch);
This is the Fsockopen approach:
$uploadInfo = Array (' Host ' => ', ' Port ' => ', ' url ' => '/upload.php ');
$fp = Fsockopen ($uploadInfo [' Host '], $uploadInfo [' Port '], $errno, $ERRSTR);
$file = ' 1.jpg ';
$content = file_get_contents ($file);
$boundary = MD5 (time ()); $out. = "--". $boundary. "
\ r \ n "; $out. = "Content-disposition:form-data; Name=\ "Uploadfile\"; Filename=\ "". $file. "
\ "\ r \ n";
$out. = "content-type:image/jpg\r\n\r\n"; $out. = $content. "
\ r \ n "; $out. = "--". $boundary. "
\ r \ n "; Fwrite ($fp, "POST". $uploadInfo [' url ']. "
Http/1.1\r\n "); Fwrite ($FP, "host:". $uploadInfo [' Host ']. "
\ r \ n "); Fwrite ($fp, "Content-type:multipart/form-data boundary=". $boundary. "
\ r \ n "); Fwrite ($FP, "Content-length:". strlen ($out). "
\r\n\r\n ");
Fwrite ($fp, $out);
while (!feof ($fp)) {$ret. = fgets ($fp, 1024);
} fclose ($FP);
$ret = Trim (Strstr ($ret, "\r\n\r\n")); Preg_match ('/http:.*/', $ret, $match);
return $match [0];
First, how to disable Fsockopen ()
Here are two common ways to disable Fsockopen.
1, modify php.ini, will disable_functions = after adding fsockopen
2. Modify PHP.ini to change allow_url_fopen = on to Allow_url_fopen = off
Ii. How to resolve the Fsockopen function is disabled
1, if the server does not disable Pfsockopen at the same time, then directly replace the Fsockopen function with Pfsockopen.
What to do: Search for a string fsockopen in the program (replaced by Pfsockopen (. Example below
Before modification:
$fp = Fsockopen ($host, $errno, $errstr, 30);
After modification:
$fp = Pfsockopen ($host, $errno, $errstr, 30);
2, if the server also disables the Pfsockopen, then replace with other functions, such as stream_socket_client (). Note: The parameters for Stream_socket_client () and Fsockopen () are different.
What to do: Search the string Fsockopen in the program (replace with Stream_socket_client (), then delete the port parameter "80" in the original Fsockopen function and add it to the $host. Example below
Before modification:
$fp = Fsockopen ($host, $errno, $errstr, 30);
After modification
$fp = Stream_socket_client ($host.) ", $errno, $errstr, 30);
3, if PHP version below 5.0,fsockopen is disabled, and there is no stream_socket_client () How to do? Write a function of your own to implement Fsockopen function, reference code:
function B_fsockopen ($host, $port, & $errno, & $errstr, $timeout) {
$ip = gethostbyname ($host);
$s = socket_create (af_inet, sock_stream, 0);
if (Socket_set_nonblock ($s)) {
$r = @socket_connect ($s, $ip, $port);
if ($r | | socket_last_error () = = einprogress) {
$errno = einprogress;
return $s;
}
$errno = Socket_last_error ($s);
$errstr = Socket_strerror ($errno);
Socket_close ($s);
return false;
}
Specific actions:
1. First find the code snippet that uses the Fsockopen function, add the above code to its top, and search for the string fsockopen in the code snippet (replaced by B_fsockopen.
2. Because the Fsockopen function returns a file pointer so it can be manipulated by a file function, but this b_fsockopen function does not return a file pointer, and you need to continue modifying the code snippet: replace the fread with the Socket_read (Socket_write ( Replace the fwrite (, with Socket_close () Replace the fclose (.
The above discussion of Php curl, fsockopen application is small to share all the content of everyone, hope to give you a reference, but also hope that we support the cloud habitat community.