terorama

PHP / curl callback

Mar 25th, 2013
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.29 KB | None | 0 0
  1. <?php
  2.  
  3.  //$t=curl_multi_getcontent ($ch);
  4. //list($header, $data) = explode("\n\n", $s4, 2);
  5.  
  6. session_start();
  7.  
  8. if (!isset($_POST['ajax'])) {
  9.    draw_page();
  10.    exit();
  11. }
  12.  
  13. if (isset($_POST['url'])) {
  14.  
  15.    if ($_POST['url']=='') {
  16.       show_test();
  17.       exit();
  18.    }
  19.  
  20.    show_image($_POST['url']);
  21. }
  22.  
  23. if (isset($_POST['status'])) {
  24.  
  25.    echo 'url: '.@$_SESSION['url'].'<br/>getheader: '.@$_SESSION['getheader'].'<br/>'.
  26.           @$_SESSION['status'];
  27.  
  28.    exit();
  29. }
  30.  
  31. //--------------------------------------------
  32. function show_image($link) {
  33.  
  34.    get_allheaders($link);
  35.    echo 'avail= '.check_avail($link).'<br/>';
  36.    echo 'is image= '.check_image($link).'<br/>';
  37.    echo '<img src="'.load_image($link).'" width="400px" /><br/>';
  38. }
  39.  
  40. //--------------------------------------------
  41.  
  42. function show_test() {
  43.  
  44. $links = array("test.jpg");
  45.  
  46.  
  47. foreach ($links as $link) {
  48.  
  49.    get_allheaders($link);
  50.    echo 'avail= '.check_avail($link).'<br/>';
  51.    echo 'is image= '.check_image($link).'<br/>';
  52.    echo '<img src="'.load_image($link).'" width="400px" /><br/>';
  53. }
  54.  
  55. }
  56. //--------------------------------------------
  57.  
  58. function draw_page() {
  59.  
  60. ?>
  61. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  62. <html xmlns="http://www.w3.org/1999/xhtml">
  63. <head>
  64.    <title>curl examples</title>
  65.  
  66.    <script type="text/javascript" src="jquery.min.js"></script>
  67.  
  68.    <script type="text/javascript">
  69.  
  70. var st, targ;
  71. //----------------------------------------
  72. function onBeforeSend(xhr, settings) {
  73.  
  74.    st.html('loading...please wait');
  75. }    
  76. //----------------------------------------
  77. function onSuccess(inf, status, xhr) {
  78.  
  79.    targ.html(inf);
  80. }
  81. //----------------------------------------
  82. function onError(xhr, status, exception) {
  83.  
  84.    targ.html('error occured<br/>status:'+status+'<br/>exception:'+exception);
  85.    
  86. }
  87. //----------------------------------------
  88. function onComplete (xhr, status) {
  89.  
  90.     st.html('request completed, status:'+status);
  91.     clearInterval(infoo);
  92. }
  93. //----------------------------------------
  94.        $(document).ready (
  95.  
  96.    function() {
  97.  
  98.     st = $('#status');
  99.     targ = $('#ajax');
  100.  
  101.    
  102.  
  103.     $('form[name=zz]').submit(
  104.      function() {
  105.         $.ajax ({
  106.             type:'POST',
  107.             url: location.href,
  108.             data: {
  109.                ajax:'',url: this.url.value
  110.             },
  111.  
  112.             beforeSend: onBeforeSend,
  113.             success: onSuccess,
  114.             error: onError,
  115.             complete: onComplete
  116.         });
  117.  
  118.      window.infoo = setInterval ( function() {
  119.  
  120.         $.ajax ({
  121.             type:'POST',
  122.             url: "stat88.php",
  123.             data: {
  124.                ajax:'',status:''
  125.             },
  126.      
  127.             success: function(inf) {
  128.  
  129.                $('#curlstatus').html(inf);
  130.  
  131.             },
  132.         });
  133.          
  134.      }, 1000);
  135.  
  136.         return false;
  137.      });
  138.  
  139.      
  140.      
  141. } )
  142.    </script>
  143.  
  144.  
  145.    <style type="text/css">
  146.    * {
  147.       margin:0;
  148.       padding:0;
  149.    }
  150.  
  151.    body {
  152.       font-family: verdana, sans-serif;
  153.       font-size: 1em;
  154.    }
  155.  
  156.     #frm {
  157.    float:left;
  158.    width:400px;
  159.    border: solid 1px #999;
  160.    padding: 20px;
  161. }
  162.  
  163.    #rez {
  164.    margin-left:450px;
  165.    
  166. }
  167.  
  168.   #status {
  169.    height: 10px;
  170.    border: solid 1px #800;
  171.    padding: 10px;
  172. }
  173.  
  174.    #ajax {
  175.    border: solid 1px #999;
  176.    padding: 10px;
  177. }
  178.  
  179. #curlstatus {
  180.    font-size: 12px;
  181.    word-wrap: break-word;
  182. }
  183.  
  184.    </style>
  185.  
  186. </head>
  187.  
  188. <body>
  189.    <div id="frm">
  190.    <form name="zz" enctype="application/x-www-form-urlencoded">
  191.    <p>input url:
  192.    <input type="text" name="url" size="50" /></p>
  193.    <input type="submit" value="send" />
  194.    </form>
  195.    <div id="curlstatus">
  196.    </div>
  197.    </div>
  198.  
  199.    <div id="rez">
  200.    <div id="status"></div>
  201.    <div id="ajax"</div>
  202.    </div>
  203.  
  204. </body>
  205. </html>
  206.  
  207.  
  208.  
  209. <?php
  210.  
  211. }
  212.  
  213. //---------------------------------------------
  214. function load_image($url) {
  215.  
  216.    $s = get_url($url);
  217.  
  218.  
  219.    return 'data:image/jpeg;base64,'.base64_encode($s);
  220. }
  221.  
  222. //---------------------------------------------
  223. function check_avail($url) {
  224.  
  225.    $s = get_url($url, true);
  226.    return $s['http_code']==200;
  227. }
  228. //---------------------------------------------
  229. function check_image($url) {
  230.  
  231.    $s = get_url($url, true);
  232.    return (substr($s['content_type'],'image')!=-1) || $s['content_type']=='application/octet-stream';
  233. }
  234.  
  235.  
  236. //---------------------------------------------
  237. function get_allheaders($url) {
  238.  
  239.     $s = get_url($url, true);
  240.     echo '<pre>'.print_r($s,true).'</pre><br/>';
  241. }
  242.  
  243. //---------------------------------------------
  244. function callback($download_size, $downloaded, $upload_size, $uploaded)
  245. {
  246.    session_start();
  247.     $_SESSION['status']= 'download size: '.$download_size.'<br/>downloaded: '.$downloaded.
  248.       '<br/>upload size:'.$upload_size.'<br/>uploaded: '.$uploaded.'<br/>';
  249.  
  250.    session_write_close();
  251. }
  252.  
  253. //---------------------------------------------
  254. function get_url($url, $header=0) {
  255.  
  256.            $_SESSION['url']=$url;
  257.            $_SESSION['getheader']=$header;
  258.            $_SESSION['status']='started<br/>';
  259.  
  260.  
  261.            $ch = curl_init();
  262.        
  263.  
  264.            curl_setopt($ch, CURLOPT_NOPROGRESS, false);
  265.            curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'callback');
  266.            curl_setopt($ch, CURLOPT_BUFFERSIZE, 128);
  267.  
  268.            
  269.        curl_setopt($ch, CURLOPT_HEADER, $header);
  270.            curl_setopt ($ch, CURLOPT_NOBODY, $header);
  271.            curl_setopt($ch, CURLOPT_URL, $url);
  272.  
  273.            curl_setopt($ch, CURLOPT_REFERER, 'http://www.google.com');
  274.            
  275.        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  276.        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  277.        curl_setopt($ch, CURLOPT_USERAGENT,
  278.           "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)");
  279.        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  280.        curl_setopt($ch,CURLOPT_ENCODING, 1);
  281.        
  282.            session_write_close();
  283.        $s4 = curl_exec($ch);
  284.  
  285.            
  286.            $s = curl_getinfo($ch);
  287.        
  288.        if (curl_errno($ch)) {
  289.           throw new Exception ('curl exception: '.curl_error($ch));      
  290.        }       
  291.        curl_close($ch);
  292.            session_start();
  293.  
  294.            if ($header==1)
  295.               return $s;
  296.            else
  297.               return $s4;  
  298.  
  299. }          
  300.  
  301.  
  302.  
  303. ?>
Add Comment
Please, Sign In to add comment