4) break; $res .= fread($fp, 8192); } $info = stream_get_meta_data($fp); fclose($fp); if ($info['timed_out']) { echo 'API: Connection timed out! $NL'; return -2; // api timeout } else { if (strlen($res) > 1000) return -3; // invalid api response (check the API website for possible problems) $data=extractBody($res); $ar=explode(":",$data); if (count($ar) < 4) return -100; // invalid api response switch ($ar[0]) { case "ERROR": echo "API Error: $res $NL"; return 0; // Error received break; case "ROTATE": $PROXY['address']=$ar[1]; $PROXY['port']=$ar[2]; $PROXY['external_ip']=$ar[3]; $PROXY['ready']=1; return 1; break; default: echo "API Error: Received answer $ar[0], expected \"ROTATE\""; return -101; // unknown API response } } } // cmd==rotate } } function dom2array($node) { $res = array(); if($node->nodeType == XML_TEXT_NODE) { $res = $node->nodeValue; } else { if($node->hasAttributes()) { $attributes = $node->attributes; if(!is_null($attributes)) { $res['@attributes'] = array(); foreach ($attributes as $index=>$attr) { $res['@attributes'][$attr->name] = $attr->value; } } } if($node->hasChildNodes()) { $children = $node->childNodes; for($i=0;$i<$children->length;$i++) { $child = $children->item($i); $res[$child->nodeName] = dom2array($child); } $res['textContent']=$node->textContent; } } return $res; } function getContent(&$NodeContent="",$nod) { $NodList=$nod->childNodes; for( $j=0 ; $j < $NodList->length; $j++ ) { $nod2=$NodList->item($j); $nodemane=$nod2->nodeName; $nodevalue=$nod2->nodeValue; if($nod2->nodeType == XML_TEXT_NODE) $NodeContent .= $nodevalue; else { $NodeContent .= "<$nodemane "; $attAre=$nod2->attributes; foreach ($attAre as $value) $NodeContent .= "{$value->nodeName}='{$value->nodeValue}'" ; $NodeContent .= ">"; getContent($NodeContent,$nod2); $NodeContent .= ""; } } } function dom2array_full($node) { $result = array(); if($node->nodeType == XML_TEXT_NODE) { $result = $node->nodeValue; } else { if($node->hasAttributes()) { $attributes = $node->attributes; if((!is_null($attributes))&&(count($attributes))) foreach ($attributes as $index=>$attr) $result[$attr->name] = $attr->value; } if($node->hasChildNodes()) { $children = $node->childNodes; for($i=0;$i<$children->length;$i++) { $child = $children->item($i); if($child->nodeName != '#text') if(!isset($result[$child->nodeName])) $result[$child->nodeName] = dom2array($child); else { $aux = $result[$child->nodeName]; $result[$child->nodeName] = array( $aux ); $result[$child->nodeName][] = dom2array($child); } } } } return $result; } function getip() { global $PROXY; if (!$PROXY['ready']) return -1; // proxy not ready $curl_handle=curl_init(); 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 curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,10); curl_setopt($curl_handle,CURLOPT_TIMEOUT,10); curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1); $curl_proxy = "$PROXY[address]:$PROXY[port]"; curl_setopt($curl_handle, CURLOPT_PROXY, $curl_proxy); $tested_ip=curl_exec($curl_handle); 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)) { curl_close($curl_handle); return $tested_ip; } else { $info = curl_getinfo($curl_handle); curl_close($curl_handle); return 0; // possible error would be a wrong authentication IP } } function new_curl_session($ch=NULL) { global $PROXY; if ((!isset($PROXY['ready'])) || (!$PROXY['ready'])) return $ch; // proxy not ready if (isset($ch) && ($ch != NULL)) curl_close($ch); $ch = curl_init(); curl_setopt ($ch, CURLOPT_HEADER, 0); curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt ($ch, CURLOPT_RETURNTRANSFER , 1); $curl_proxy = "$PROXY[address]:$PROXY[port]"; curl_setopt($ch, CURLOPT_PROXY, $curl_proxy); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20); curl_setopt($ch, CURLOPT_TIMEOUT, 20); 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"); return $ch; } ?>