Introduction to the application of curl and fsockopen in php, curlfsockopen
Recently, files uploaded through post are used. The post submission and fsockopen of curl are carried online. Among them, curl is the simplest, so it is 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);
This option CURLOPT_RETURNTRANSFER: if it is set to true/1, the content of the requested webpage is not automatically output to the screen during curl_exec, and $ ret is the content of the requested webpage, if it is set to false/0, the content of the requested webpage is automatically output to the screen during curl_exec. If the request is successful, the content of $ ret is 1 or true.
The following is the code for uploading local files. If you need to upload a remote file, go down to the local directory and delete it. (If you have other methods, please inform us ):
$ Url = ''; $ file = '1.jpg '; $ field ['uploadfile'] = '@'. $ file; (uploadFile is the name of the receiver) $ 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 method:
$uploadInfo = array( 'host'=>'', 'port'=>'80', '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];
1. How to disable fsockopen ()
The following are two common methods to disable fsockopen.
1. Modify php. ini and add disable_functions = to fsockopen.
2. Modify php. ini and change allow_url_fopen = On to allow_url_fopen = Off.
Ii. how to disable the fsockopen Function
1. If the server does not disable pfsockopen at the same time, replace the fsockopen function with pfsockopen.
Specific Operation: search for the string fsockopen in the Program (replace with pfsockopen (. Example:
Before modification:
$fp = fsockopen($host, 80, $errno, $errstr, 30);
After modification:
$fp = pfsockopen($host, 80, $errno, $errstr, 30);
2. If pfsockopen is disabled on the server, use other functions, such as stream_socket_client (). Note: The parameters of stream_socket_client () and fsockopen () are different.
Specific Operation: Search for the character string fsockopen in the Program (replace it with stream_socket_client (, and then delete the port parameter "80" in the original fsockopen function and add it to $ host. Example:
Before modification:
$fp = fsockopen($host, 80, $errno, $errstr, 30);
After modification
$fp = stream_socket_client($host."80", $errno, $errstr, 30);
3. What if fsockopen is disabled and stream_socket_client () is not available if PHP version is earlier than 5.0? Write a function to implement the fsockopen function. Refer to the 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 operations:
1. First find the code segment that uses the fsockopen function, add the code above to its top, and search for the string fsockopen in the code segment (replace it with B _fsockopen (.
2. because the fsockopen function returns the file pointer, it can be operated by the file function, but this B _fsockopen function does not return the file pointer. You need to modify the code segment: Use socket_read (replace fread (, use socket_write (replace fwrite (and use socket_close (replace fclose (.
In the above discussion, the application of curl and fsockopen in php is all the content that I have shared with you. I hope to give you a reference and support for the customer's house.