PageRenderTime 64ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/TEST/PROXY/GOOGLE/functions.php

https://bitbucket.org/LogIN/appstorez
PHP | 229 lines | 196 code | 24 blank | 9 comment | 39 complexity | 5f56ff8216340cd2152ab92c4adc9c80 MD5 | raw file
  1. <?php
  2. /* This code is free to use and modify as long as this comment is untouched
  3. * Original source and details: http://google-scraper.squabbel.com
  4. * All rights reserves, justone@squabbel.com
  5. */
  6. /*
  7. * This is the API function for www.seo-proxies.com, currently supporting the "rotate" command
  8. * On success it will define the $PROXY variable, adding the elements ready,address,port,external_ip and return 1
  9. * On failure the return is <= 0 and the PROXY variable ready element is set to "0"
  10. */
  11. function extractBody($response_str)
  12. {
  13. $parts = preg_split('|(?:\r?\n){2}|m', $response_str, 2);
  14. if (isset($parts[1])) return $parts[1];
  15. return '';
  16. }
  17. function proxy_api($cmd,$x="")
  18. {
  19. global $pwd;
  20. global $uid;
  21. global $PROXY;
  22. global $NL;
  23. $fp = fsockopen("www.seo-proxies.com", 80);
  24. if (!$fp)
  25. {
  26. echo "Unable to connect to proxy API $NL";
  27. return -1; // connection not possible
  28. } else
  29. {
  30. if ($cmd == "rotate")
  31. {
  32. $PROXY['ready']=0;
  33. fwrite($fp, "GET /api.php?api=1&uid=$uid&pwd=$pwd&cmd=rotate&randomness=1 HTTP/1.0\r\nHost: www.seo-proxies.com\r\nAccept: text/html, text/plain, text/*, */*;q=0.01\r\nAccept-Encoding: plain\r\nAccept-Language: en\r\n\r\n");
  34. stream_set_timeout($fp, 8);
  35. $res="";
  36. $n=0;
  37. while (!feof($fp))
  38. {
  39. if ($n++ > 4) break;
  40. $res .= fread($fp, 8192);
  41. }
  42. $info = stream_get_meta_data($fp);
  43. fclose($fp);
  44. if ($info['timed_out'])
  45. {
  46. echo 'API: Connection timed out! $NL';
  47. return -2; // api timeout
  48. } else
  49. {
  50. if (strlen($res) > 1000) return -3; // invalid api response (check the API website for possible problems)
  51. $data=extractBody($res);
  52. $ar=explode(":",$data);
  53. if (count($ar) < 4) return -100; // invalid api response
  54. switch ($ar[0])
  55. {
  56. case "ERROR":
  57. echo "API Error: $res $NL";
  58. return 0; // Error received
  59. break;
  60. case "ROTATE":
  61. $PROXY['address']=$ar[1];
  62. $PROXY['port']=$ar[2];
  63. $PROXY['external_ip']=$ar[3];
  64. $PROXY['ready']=1;
  65. return 1;
  66. break;
  67. default:
  68. echo "API Error: Received answer $ar[0], expected \"ROTATE\"";
  69. return -101; // unknown API response
  70. }
  71. }
  72. } // cmd==rotate
  73. }
  74. }
  75. function dom2array($node)
  76. {
  77. $res = array();
  78. if($node->nodeType == XML_TEXT_NODE)
  79. {
  80. $res = $node->nodeValue;
  81. } else
  82. {
  83. if($node->hasAttributes())
  84. {
  85. $attributes = $node->attributes;
  86. if(!is_null($attributes))
  87. {
  88. $res['@attributes'] = array();
  89. foreach ($attributes as $index=>$attr)
  90. {
  91. $res['@attributes'][$attr->name] = $attr->value;
  92. }
  93. }
  94. }
  95. if($node->hasChildNodes())
  96. {
  97. $children = $node->childNodes;
  98. for($i=0;$i<$children->length;$i++)
  99. {
  100. $child = $children->item($i);
  101. $res[$child->nodeName] = dom2array($child);
  102. }
  103. $res['textContent']=$node->textContent;
  104. }
  105. }
  106. return $res;
  107. }
  108. function getContent(&$NodeContent="",$nod)
  109. {
  110. $NodList=$nod->childNodes;
  111. for( $j=0 ; $j < $NodList->length; $j++ )
  112. {
  113. $nod2=$NodList->item($j);
  114. $nodemane=$nod2->nodeName;
  115. $nodevalue=$nod2->nodeValue;
  116. if($nod2->nodeType == XML_TEXT_NODE)
  117. $NodeContent .= $nodevalue;
  118. else
  119. { $NodeContent .= "<$nodemane ";
  120. $attAre=$nod2->attributes;
  121. foreach ($attAre as $value)
  122. $NodeContent .= "{$value->nodeName}='{$value->nodeValue}'" ;
  123. $NodeContent .= ">";
  124. getContent($NodeContent,$nod2);
  125. $NodeContent .= "</$nodemane>";
  126. }
  127. }
  128. }
  129. function dom2array_full($node)
  130. {
  131. $result = array();
  132. if($node->nodeType == XML_TEXT_NODE)
  133. {
  134. $result = $node->nodeValue;
  135. } else
  136. {
  137. if($node->hasAttributes())
  138. {
  139. $attributes = $node->attributes;
  140. if((!is_null($attributes))&&(count($attributes)))
  141. foreach ($attributes as $index=>$attr)
  142. $result[$attr->name] = $attr->value;
  143. }
  144. if($node->hasChildNodes())
  145. {
  146. $children = $node->childNodes;
  147. for($i=0;$i<$children->length;$i++)
  148. {
  149. $child = $children->item($i);
  150. if($child->nodeName != '#text')
  151. if(!isset($result[$child->nodeName]))
  152. $result[$child->nodeName] = dom2array($child);
  153. else
  154. {
  155. $aux = $result[$child->nodeName];
  156. $result[$child->nodeName] = array( $aux );
  157. $result[$child->nodeName][] = dom2array($child);
  158. }
  159. }
  160. }
  161. }
  162. return $result;
  163. }
  164. function getip()
  165. {
  166. global $PROXY;
  167. if (!$PROXY['ready']) return -1; // proxy not ready
  168. $curl_handle=curl_init();
  169. curl_setopt($curl_handle,CURLOPT_URL,'http://squabbel.com/ipxx.php'); // this site will return the plain IP address, great for testing if a proxy is ready
  170. curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,10);
  171. curl_setopt($curl_handle,CURLOPT_TIMEOUT,10);
  172. curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
  173. $curl_proxy = "$PROXY[address]:$PROXY[port]";
  174. curl_setopt($curl_handle, CURLOPT_PROXY, $curl_proxy);
  175. $tested_ip=curl_exec($curl_handle);
  176. if(preg_match("^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}^", $tested_ip))
  177. {
  178. curl_close($curl_handle);
  179. return $tested_ip;
  180. }
  181. else
  182. {
  183. $info = curl_getinfo($curl_handle);
  184. curl_close($curl_handle);
  185. return 0; // possible error would be a wrong authentication IP
  186. }
  187. }
  188. function new_curl_session($ch=NULL)
  189. {
  190. global $PROXY;
  191. if ((!isset($PROXY['ready'])) || (!$PROXY['ready'])) return $ch; // proxy not ready
  192. if (isset($ch) && ($ch != NULL))
  193. curl_close($ch);
  194. $ch = curl_init();
  195. curl_setopt ($ch, CURLOPT_HEADER, 0);
  196. curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
  197. curl_setopt ($ch, CURLOPT_RETURNTRANSFER , 1);
  198. $curl_proxy = "$PROXY[address]:$PROXY[port]";
  199. curl_setopt($ch, CURLOPT_PROXY, $curl_proxy);
  200. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
  201. curl_setopt($ch, CURLOPT_TIMEOUT, 20);
  202. curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.0; en; rv:1.9.0.4) Gecko/2009011913 Firefox/3.0.6");
  203. return $ch;
  204. }
  205. ?>