PHP - 利用Curl、socket、file_get_contents POST数据

// php的三种方法向远程服务器POST数据

<?php     
/**     
* Socket版本
     
* 使用方法:     
* $post_string = "app=socket&version=beta";     
* request_by_socket('facebook.cn','/restServer.php',$post_string);     
*/     
function request_by_socket($remote_server,$remote_path,$post_string,$port = 80,$timeout = 30){     
     $socket = fsockopen($remote_server,$port,$errno,$errstr,$timeout);     
     if (!$socket) die("$errstr($errno)");     
         
     fwrite($socket,"POST $remote_path HTTP/1.0");     
     fwrite($socket,"User-Agent: Socket Example");     
     fwrite($socket,"HOST: $remote_server");     
     fwrite($socket,"Content-type: application/x-www-form-urlencoded");     
     fwrite($socket,"Content-length: ".strlen($post_string)+8."");     
     fwrite($socket,"Accept:*/*");     
     fwrite($socket,"");     
     fwrite($socket,"mypost=$post_string");     
     fwrite($socket,"");     
         
     $header = "";     
     while ($str = trim(fgets($socket,4096))) {     
             $header.=$str;     
     }     
         
     $data = "";     
     while (!feof($socket)) {     
             $data .= fgets($socket,4096);     
     }     
         
     return $data;     
}     
     
     
/**     
* Curl版本     
* 使用方法:     
* $post_string = "app=request&version=beta";     
* request_by_curl('http://facebook.cn/restServer.php',$post_string);     
*/     
function request_by_curl($remote_server,$post_string){     
     $ch = curl_init();     
     curl_setopt($ch,CURLOPT_URL,$remote_server);     
     curl_setopt($ch,CURLOPT_POSTFIELDS,'mypost='.$post_string);     
     curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);     
     curl_setopt($ch,CURLOPT_USERAGENT,"Jimmy's CURL Example beta");     
     $data = curl_exec($ch);     
     curl_close($ch);     
     return $data;     
}     
/**     
* 其它版本   
 
* 使用方法:     
* $post_string = "app=request&version=beta";     
* request_by_other('http://facebook.cn/restServer.php',$post_string);     
*/     
function request_by_other($remote_server,$post_string){     
     $context = array(     
             'http'=>array(     
                 'method'=>'POST',     
                 'header'=>'Content-type: application/x-www-form-urlencoded'."".     
                             'User-Agent : Jimmy's POST Example beta'."".     
                             'Content-length: '.strlen($post_string)+8,     
                 'content'=>'mypost='.$post_string)     
             );     
     $stream_context = stream_context_create($context);     
     $data = file_get_contents($remote_server,FALSE,$stream_context);     
     return $data;     
}     
     
?>

PHP生成网页快照

 

<?php

$url = 'www.baidu.com'; //抓取百度

echo snapshot($url);  //输出结果为图片地址

echo snapshot($url, './baidu.png'); //将图片保存至本地baidu.png, 输出内容图片大小

/**

 * 生成网页快照

 * Fr PHP100.com 论坛 xiaokai

 * @param   string  $site   目标地址

 * @param   string  $path   保存地址, 为空则不保存

 * @param   integer $dealy  延迟

 * @return  mixed   根据参数返回

 */

function snapshot($site, $path = '', $dealy = 0)

{

    $url   = 'http://ppt.cc/yo2/catch.php';

    $query = 'url=' . $site . '&delay=' . $dealy . '&rnd=' . mt_rand(1, 9);

    $ch    = curl_init($url);

    curl_setopt($ch, CURLOPT_POST, true);

    curl_setopt($ch, CURLOPT_POSTFIELDS, $query);

    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $data = curl_exec($ch);

    curl_close($ch);



    if (strlen($data) != 32) {

        exit('无效网址');

    }



    $file = $data{0} . '/' . $data{1} . '/' . $data{2} . '/';

    $file = 'http://cache.ppt.cc/' . $file . 'src_' . $data . '.png';



    if (!empty($path)) {

        $data = file_get_contents($file);

        return file_put_contents($path, $data);

    }

    return $file;

}

用JS onpaste事件禁止向控件粘贴内容

<inputname="mobileNo1"class="width150px"tabindex="2"type="text"maxlength="11"value=""onpaste="return false;"/> 

onpaste Event

Fires on the target object when the user pastes data, transferring the data from the system clipboard to the document.

Platform Support
IE   Netscape   Mozilla       Opera   Safari
4.0+   no             no     no             1.0+

当用户从系统剪贴板粘贴数据、传输数据时,激活此事件。

 

用Ctrl+拖动,可以将内容复制过来

标签: js, 数据, data, onpaste