Last active
March 15, 2019 19:37
-
-
Save id4ehsan/3ff59ee17192f73c0818972eade6298d to your computer and use it in GitHub Desktop.
Bonbast.com to json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
set_time_limit(0); | |
ini_set('max_execution_time', 0); | |
function get_remote_data($url, $post_paramtrs=false, $return_full_array=false) { | |
$c = curl_init();curl_setopt($c, CURLOPT_URL, $url); | |
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); | |
//if parameters were passed to this function, then transform into POST method.. (if you need GET request, then simply change the passed URL) | |
if($post_paramtrs){curl_setopt($c, CURLOPT_POST,TRUE); curl_setopt($c, CURLOPT_POSTFIELDS, "var1=bla&".$post_paramtrs );} | |
curl_setopt($c, CURLOPT_SSL_VERIFYHOST,false); | |
curl_setopt($c, CURLOPT_SSL_VERIFYPEER,false); | |
curl_setopt($c, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; rv:33.0) Gecko/20100101 Firefox/33.0"); | |
curl_setopt($c, CURLOPT_COOKIE, 'CookieName1=Value;'); | |
//We'd better to use the above command, because the following command gave some weird STATUS results.. | |
$header[0]= $user_agent="User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:33.0) Gecko/20100101 Firefox/33.0"; $header[]="Cookie:CookieName1=Value;"; $header[]="Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"; $header[]="Cache-Control: max-age=0"; $header[]="Connection: keep-alive"; $header[]="Keep-Alive: 300"; $header[]="Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"; $header[] = "Accept-Language: en-us,en;q=0.5"; $header[] = "Pragma: "; curl_setopt($c, CURLOPT_HEADER, true); curl_setopt($c, CURLOPT_HTTPHEADER, $header); | |
curl_setopt($c, CURLOPT_MAXREDIRS, 10); | |
//if SAFE_MODE or OPEN_BASEDIR is set,then FollowLocation cant be used.. so... | |
$follow_allowed= ( ini_get('open_basedir') || ini_get('safe_mode')) ? false:true; if ($follow_allowed){curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);} | |
curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 9); | |
curl_setopt($c, CURLOPT_REFERER, $url); | |
curl_setopt($c, CURLOPT_TIMEOUT, 60); | |
curl_setopt($c, CURLOPT_AUTOREFERER, true); | |
curl_setopt($c, CURLOPT_ENCODING, 'gzip,deflate'); | |
$data=curl_exec($c);$status=curl_getinfo($c);curl_close($c); | |
preg_match('/(http(|s)):\/\/(.*?)\/(.*\/|)/si', $status['url'],$link); | |
//correct assets URLs(i.e. retrieved url is: http://example.com/DIR/SUBDIR/page.html... then href="./image.JPG" becomes href="http://example.com/DIR/SUBDIR/image.JPG", but href="/image.JPG" needs to become href="http://example.com/image.JPG") | |
//inside all links(except starting with HTTP,javascript:,HTTPS,//,/ ) insert that current DIRECTORY url (href="./image.JPG" becomes href="http://example.com/DIR/SUBDIR/image.JPG") | |
$data=preg_replace('/(src|href|action)=(\'|\")((?!(http|https|javascript:|\/\/|\/)).*?)(\'|\")/si','$1=$2'.$link[0].'$3$4$5', $data); | |
//inside all links(except starting with HTTP,javascript:,HTTPS,//) insert that DOMAIN url (href="/image.JPG" becomes href="http://example.com/image.JPG") | |
$data=preg_replace('/(src|href|action)=(\'|\")((?!(http|https|javascript:|\/\/)).*?)(\'|\")/si','$1=$2'.$link[1].'://'.$link[3].'$3$4$5', $data); | |
// if redirected, then get that redirected page | |
if($status['http_code']==301 || $status['http_code']==302) { | |
//if we FOLLOWLOCATION was not allowed, then re-get REDIRECTED URL | |
//p.s. WE dont need "else", because if FOLLOWLOCATION was allowed, then we wouldnt have come to this place, because 301 could already auto-followed by curl :) | |
if (!$follow_allowed){ | |
//if REDIRECT URL is found in HEADER | |
if(empty($redirURL)){if(!empty($status['redirect_url'])){$redirURL=$status['redirect_url'];}} | |
//if REDIRECT URL is found in RESPONSE | |
if(empty($redirURL)){preg_match('/(Location:|URI:)(.*?)(\r|\n)/si', $data, $m); if (!empty($m[2])){ $redirURL=$m[2]; } } | |
//if REDIRECT URL is found in OUTPUT | |
if(empty($redirURL)){preg_match('/moved\s\<a(.*?)href\=\"(.*?)\"(.*?)here\<\/a\>/si',$data,$m); if (!empty($m[1])){ $redirURL=$m[1]; } } | |
//if URL found, then re-use this function again, for the found url | |
if(!empty($redirURL)){$t=debug_backtrace(); return call_user_func( $t[0]["function"], trim($redirURL), $post_paramtrs);} | |
} | |
} | |
// if not redirected,and nor "status 200" page, then error.. | |
elseif ( $status['http_code'] != 200 ) { $data = "ERRORCODE22 with $url<br/><br/>Last status codes:".json_encode($status)."<br/><br/>Last data got:$data";} | |
return ( $return_full_array ? array('data'=>$data,'info'=>$status) : $data); | |
} | |
//echo "<textarea"." style=".'"margin: 0px; width: 776px; height: 580px;"'.">"; | |
$data = get_remote_data("https://bonbast.com/"); | |
//echo $data; | |
preg_match_all('|<td>\s*<a\s+href="[^"]+">\s*<img\s+class="[^"]+"\s+alt="[^"]+"\s+src="[^"]+"\s+/>\s*([A-Z]+)</a>\s*</td>\s*<td>\s*([^<]+)</td>\s*<td\s+id="[^"]+"\s+class="[^"]+">([0-9]+)</td>\s*<td\s+id="[^"]*"\s+class="[^"]*">([0-9]+)</td>|isU', $data, $outs, PREG_SET_ORDER); | |
//echo $data; | |
$tmparray2 = array(); | |
foreach ($outs as $out){ | |
// echo $out[1] . " ".$out[2] . " ".$out[3] . " " . $out[4] . " " . "\n"; | |
$tmparray = array('countryName'=>$out[2], 'cUnit'=>$out[1], 'rate'=>array('cBuying'=>$out[4],'cSelling'=>$out[3])); | |
array_push($tmparray2, $tmparray); | |
} | |
$tojson = array('data'=>array('exchangeRate'=>$tmparray2)); | |
//print_r($outs); | |
//print_r($tojson); | |
$myJSON = json_encode($tojson); | |
echo $myJSON; | |
//echo "</textarea>"; | |
//echo "<p>Finnished</p>"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment