Advertisement
Guest User

getData

a guest
Jan 10th, 2013
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.81 KB | None | 0 0
  1. /*fetches a page
  2. $url => url of the page,
  3. $data =>array(<param1>=><value1>,<param2>=><value2>....) storing get or post pvariables and values
  4. $cookie => array('cookieName1'=>value1,'cookieName2'=>'value2'......)//storing cookies and their values
  5. $userAgent=>userAgent,
  6. method =>HTTP method [GET/POSt]
  7. */
  8. function fetchPage( $url,$data ,$cookie='',$userAgent ='',$method = 'GET') //sends data array(param=>val,...) to the page $url in post method and returns the reply string
  9. {
  10.     $httpArr = array();
  11.     $header = "Accept-language: en\r\n";
  12.     if($method != 'GET'){
  13.         $method = 'POST';
  14.         $data    = http_build_query( $data );
  15.         $header = $header."Content-Type: application/x-www-form-urlencoded\r\n" .
  16.                    "Content-Length: " . strlen( $data ).'\r\n';
  17.         $content = $data;          
  18.     } else {
  19.         $content = '';
  20.         $queryArr = array();
  21.         foreach($data as $key => $value) {
  22.             array_push($queryArr,$key.'='.$value);
  23.         }
  24.         $url = $url.'?'.implode('&',$queryArr);
  25.     }
  26.     if($cookie != '') {
  27.         $cookieStr = 'Cookie: ';
  28.         $len = sizeof($cookie);
  29.         $i = 0;
  30.         foreach($cookie as $key => $val) {
  31.              $cookieStr = $cookieStr.$key.'='.$val;
  32.              $cookieStr = $i != $len - 1 ?$cookieStr.'; ':$cookieStr;
  33.              $i++;
  34.         }
  35.     }
  36.     $userAgent = $userAgent !='' ? $userAgent:'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)';
  37.     $header = $header.'\r\n'.$userAgent.$cookie;
  38.     $httpArr['method'] = $method;
  39.     $httpArr['follow_location'] = true;
  40.     $httpArr['header'] = $header;
  41.     $context = stream_context_create( array(
  42.          "http" => $httpArr
  43.     ) );
  44.     $page    = file_get_contents( $url, false, $context );
  45.     var_dump($http_response_header);
  46.     return $page;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement