/** @brief URL wrapper class to encapsulate common URL request operations **/classurl{Private $error; public function__construct () {$this->error =NewErrorcase (); } /** * combineurl * Stitching URL * @param string $baseURL based URL * @param array $keysArr List of parameters * @retu RN string returns the URL of the stitching*/ public functionCombineurl ($baseURL,$KEYSARR){ $combined=$baseURL."?"; $VALUEARR=Array(); foreach($KEYSARR as $key=$val){ $VALUEARR[] = "$key=$val"; } $keyStr=implode("&",$VALUEARR); $combined.= ($keyStr); return $combined; } /** * get_contents * server obtains content via GET request * @param string $url requested url, post-stitched * @return String request What's returned*/ public functionGet_contents ($url){ if(Ini_get("allow_url_fopen") = = "1") { $response=file_get_contents($url); }Else{ $ch=Curl_init (); curl_setopt ($ch, curlopt_ssl_verifypeer,FALSE); curl_setopt ($ch, curlopt_returntransfer,TRUE); curl_setopt ($ch, curlopt_url,$url); $response= Curl_exec ($ch); Curl_close ($ch); } //-------request is empty if(Empty($response)){ $this->error->showerror ("50001"); } return $response; } /** Get * Get method Request resource * @param string $url based on BaseURL * @param array $keysArr List of parameters * @r Eturn string returns the contents of the resource*/ public functionGet$url,$KEYSARR){ $combined=$this->combineurl ($url,$KEYSARR); return $this->get_contents ($combined); } /** * Post * POST method Request resource * @param string $url based on BaseURL * @param array $KEYSARR The requested parameter list * @ param int $flag Flag bit * @return The contents of the resource returned by string*/ public functionPost$url,$KEYSARR,$flag= 0){ $ch=Curl_init (); if(!$flag) curl_setopt ($ch, curlopt_ssl_verifypeer,FALSE); curl_setopt ($ch, curlopt_returntransfer,TRUE); curl_setopt ($ch, curlopt_post,TRUE); curl_setopt ($ch, curlopt_postfields,$KEYSARR); curl_setopt ($ch, curlopt_url,$url); $ret= Curl_exec ($ch); Curl_close ($ch); return $ret; }}
Php-to-url-action classes: URL stitching, get fetch page, post fetch page (with pass Parameter)