Latest: PHP cURL Class Example Main Module Source: The class is initialized and called like this: function example() { $testcurl = new cURLCore; $testcurl->url = "http://localhost/testfolder/expenses.xml"; $testcurl->leftpx = 20; $testcurl->toppx = 210; $testcurl->container = "containerID"; $testcurl->showerrors = true; $testcurl->showheader = true; $testcurl->customheader = array("Expenses", "Q1", "Q2", "Q3", "Q4"); $testcurl->tableborder = 1; $testcurl->smallerfont = true; $testcurl->cURLCoreInit(); $testcurl = null; } (Note properties have defaults that are optionally overridden when each instance is created, as above.) PHP cURL Class Source: <?php /*** ** AjaMyAjax --> my PHP cURL, data formatting class library ** ** This software provided "AS IS," but is FREE to use and share. ** For details see the as-is doc and Creative Commons license: ** http://creativecommons.org/licenses/by-sa/3.0/ ** ** More PHP cURL examples, AJAX, and other apps here: ** http://AjaMyAjax.com, (c) AjaMyAjax.com */ /** ** constructor and public variables: */ class cURLCore { var $url = ""; var $output = ""; var $container = ""; var $leftpx = 0; var $toppx = 0; var $showerrors = true; var $showheader = false; var $customheader = array(); var $returndata = false; var $smallerfont = false; var $rcornervalue = false; var $cwidth = ""; var $tableborder = 0; var $tablemaxperrow = 0; var $tableheaderalign = "center"; var $tablerowalign = "left"; var $tablecellalign = "left"; var $tablecaption = ""; var $tablestr = ""; var $tdcnt = 0; public function cURLCoreInit() { if (!function_exists("curl_init")) { if ($this->showerrors) echo "Error: PHP cURL library not loaded"; exit; } // xsl routines do their own file handling if ((strpos(strtolower($this->url), '.xsl') !== false)) { $this->output = $this->format_xsl(); if ($this->returndata) return $this->output; else $this->show_data(); } else { $ch = curl_init(); if (!curl_errno($ch)) { curl_setopt($ch, CURLOPT_URL, $this->url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 10); $this->output = curl_exec($ch); // returned output string $info = curl_getinfo($ch); if ($this->output === false || $info['http_code'] != 200) { // set display string to error text, if not ok status: if ($this->showerrors) { $this->output = "No cURL data returned for $this->url [". $info['http_code']. "]"; if (curl_error($ch)) $this->output .= "\n". curl_error($ch); } else // note: system error return in this var; you might want to use $this->output = null; } else { // pre-format selected output: if ((strpos(strtolower($this->url), '.xml') !== false)) { $this->output = $this->format_xml($this->output); } elseif ((strpos(strtolower($this->url), '.json') !== false)) { $this->output = $this->format_json($this->output); } } if ($this->output != null) { if ($this->returndata) return $this->output; else $this->show_data(); } } else { if ($this->showerrors) echo "Critical PHP cURL error". curl_error($ch); } curl_close($ch); $ch = null; } } private function show_data() { if (empty($this->rcornervalue)) { $pxloc = "<span id='"; $pxloc .= (!empty($this->container)) ? $this->container : "mycURLid"; if (!empty($this->leftpx) && !empty($this->toppx)) { $pxloc .= "' style='position:absolute;left:". $this->leftpx. "px;top:". $this->toppx. "px;"; } // add any passed custom width parameter: if (!empty($this->cwidth)) { $pxloc .= "width:". $this->cwidth; if ((strpos($this->cwidth, 'px') === false)) { $pxloc .= "px"; } $pxloc .= ";"; } $pxloc .= "'>"; echo $pxloc. $this->output; echo "</span>"; $pxloc = null; } else { $cornerfmt = "<span id='rcornerID'"; if (!empty($this->leftpx) && !empty($this->toppx)) { $cornerfmt .= " style='position:absolute;left:". $this->leftpx. "px;top:". $this->toppx. "px;"; } // add any passed custom width parameter: if (!empty($this->cwidth)) { $cornerfmt .= "width:". $this->cwidth; if ((strpos($this->cwidth, 'px') === false)) { $cornerfmt .= "px"; } $cornerfmt .= ";"; } $cornerfmt .= "'>"; $cornerfmt .= "<b id='f1'></b><b id='f2'></b><b id='f3'></b><b id='f4'></b>"; $cornerfmt .= "<div id='fcontent'><span id='"; $cornerfmt .= (!empty($container)) ? $container : ""; $cornerfmt .= "'>". $this->output; $cornerfmt .= "</span></div>"; $cornerfmt .= "<b id='f4'></b><b id='f3'></b><b id='f2'></b><b id='f1'></b>"; $cornerfmt .= "</span>"; // insert any passed custom background color parameter: if ((strpos($this->rcornervalue, '#') !== false)) { $cornerfmt = str_replace("div id=", "div style='background-color: ". $this->rcornervalue. ";' id=", $cornerfmt); $cornerfmt = str_replace("b id=", "b style='background-color: ". $this->rcornervalue. ";' id=", $cornerfmt); } echo $cornerfmt; $cornerfmt = null; } $this->output = null; flush(); } private function format_xml($str) { if ($str) { $root = simplexml_load_string($str); if ($root !== false) { $this->tablestr = "<table id='tablebody' border='". $this->tableborder. "'>"; if (!empty($this->tablecaption)) $this->tablestr .= "<caption>". $this->tablecaption. "</caption>"; if ($this->showheader) { $tblhdr = ""; if (is_array($this->customheader) && isset($this->customheader)) { foreach ($this->customheader as $hdrstr) { $tblhdr .= "<td>". $hdrstr. "</td>"; } } else { $tblhdr = ucwords(strtolower($root->getName())); // proper case } if (!empty($tblhdr)) $this->tablestr .= "<tr id='tablehdr' align='". $this->tableheaderalign. "'>". $tblhdr. "</tr>"; } $this->tablestr .= "<tr id='tablerow' align='". $this->tablerowalign. "'>"; $this->tdcnt = 0; $this->format_node($root); $this->tablestr .= "</tr></table>"; $root = null; } else $this->tablestr = "Error parsing xml document"; return $this->tablestr; } else return $str; } private function format_node($node) { $fontsizecnt = 1; foreach ($node as $element) { $elstr = trim($element); if (!empty($elstr)) { if ($this->smallerfont) { $this->tablestr .= "<td align='". $this->tablecellalign. "'><font size='-". $fontsizecnt. "'>". $this->addAnchorTag($element). "</font></td>"; } else { $this->tablestr .= "<td align='". $this->tablecellalign. "'>". $this->addAnchorTag($element). "</td>"; } $this->tdcnt++; // split table at max per row, if set if ($this->tablemaxperrow > 0 && $this->tdcnt >= $this->tablemaxperrow) { $this->tablestr .= "</tr><tr id='tablerow'>"; $this->tdcnt=0; } } if ($element->children()) { $this->tablestr .= "</tr><tr id='tablerow'>"; $this->format_node($element); } } $fontsizecnt += 1; } private function format_xsl() { if (!extension_loaded("xsl")) { return "xsl extension not loaded, cannot process ". $this->url; } $xmlfile = str_replace('.xsl', '.xml', $this->url); $xml = new DomDocument(); if (!@$xml->load($xmlfile)) return "xml file error:". $xmlfile; $xsl = new DomDocument; if (!@$xsl->load($this->url)) return "xsl file error:". $this->url; // $xsl->load($this->url, LIBXML_NOCDATA); $xslProcess = new xsltprocessor(); if (@$xslProcess->importStyleSheet($xsl)) $results = $xslProcess->transformToXML($xml); else $results = "XSLT transformation not complete: ". $this->url; $xmlfile = null; $xml = null; $xsl = null; $xslProcess = null; return $results; } private function format_json($str) { $jsonstr = preg_replace('/[\{:,"]/', '', $str); $jsonstr = preg_replace('/[\[\]}]/', '<br/>', $jsonstr); return $jsonstr; } private function addAnchorTag($str) { $findstr = strtolower($str); $startpos = strpos($findstr, "http:"); if ($startpos === false) return $str; $ahrefpos = strpos($findstr, "a href"); if ($ahrefpos !== false) return $str; if ($startpos >= 0) { $endpos = $startpos + 7; // http://... // try to find end of anchor text $regexpr = "/[\s,:;\[\]{}'\"]/"; $slen = strlen($findstr); while ($endpos < $slen) { $schar = substr($findstr, $endpos, 1); if (preg_match($regexpr, $schar)) break; $endpos++; } if ($endpos > $startpos && ($endpos - $startpos) > 11) { $anchor = substr($str, $startpos, $endpos - $startpos); $stranchor = "<a href='". $anchor. "'>". $anchor. "</a>"; $strmod = substr($str, 0, $startpos). $stranchor. substr($str, $endpos, strlen($str)); } } if (!$strmod) $strmod = $str; return $strmod; } } // end: cURLCore class ?>