<?xml version="1.0" encoding="UTF-8"?> <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" ><channel><title>TECKpert &#187; PHP</title> <atom:link href="http://www.teckpert.com/category/php/feed/" rel="self" type="application/rss+xml" /><link>http://www.teckpert.com</link> <description>Your web and software development experts</description> <lastBuildDate>Wed, 18 Jan 2012 17:38:06 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.1.3</generator> <item><title>Highrise &amp; Cforms II &#8211; A story of integration</title><link>http://www.teckpert.com/api/highrise-cforms-ii-a-story-of-integration/</link> <comments>http://www.teckpert.com/api/highrise-cforms-ii-a-story-of-integration/#comments</comments> <pubDate>Thu, 18 Nov 2010 20:54:07 +0000</pubDate> <dc:creator>Andres</dc:creator> <category><![CDATA[API]]></category> <category><![CDATA[Blog]]></category> <category><![CDATA[Coding Tips]]></category> <category><![CDATA[PHP]]></category><guid isPermaLink="false">http://www.teckpert.com/?p=1279</guid> <description><![CDATA[There is an excellent plugin available by the name of WP Highrise Contact which gives users the ability to insert a quick tag and generate a form on any page, normally this would be more than sufficient for CRM Integration but a lot of us love Cforms for our forms]]></description> <content:encoded><![CDATA[<p>There is an excellent plugin available by the name of <a href="http://wordpress.org/extend/plugins/wp-highrise-contact/" target="_blank">WP Highrise Contact</a> which gives users the ability to insert a quick tag and generate a form on any page, normally this would be more than sufficient for CRM Integration but a lot of us love Cforms for our forms. Recently the need to manage incoming leads in a more efficient manner was needed and we&#8217;d been using Cforms and saw WP Highrise and thought that the two could be made to work together, the end result is a bit on the &#8220;hackish&#8221; but saved us the work of developing a custom plugin to hook into Cforms. This is how.</p><p>First we need to examine the way that Cforms sends data,</p><p><img class="alignnone size-full wp-image-1280" title="2010-11-18_1522" src="http://assets.teckpert.com/wp-content/uploads/2010/11/2010-11-18_1522.png" alt="2010-11-18_1522" width="593" height="247" /></p><p>We see that data is sent according to the way that the form has been setup in Cform&#8217;s Administration panel. While we&#8217;re looking at this image, notice <strong>cf7_field_7 </strong>it&#8217;s a bit odd and it&#8217;s what&#8217;s responsible for mapping out Cform fields with WP Highrise Contact.</p><p><img class="alignnone size-full wp-image-1281" title="2010-11-18_1526" src="http://assets.teckpert.com/wp-content/uploads/2010/11/2010-11-18_1526.png" alt="2010-11-18_1526" width="525" height="212" /></p><p>By default the WP Highrise Contact plugin accepts only a person&#8217;s name, a company, an email, a phone number and a website as well as a message. Here we see that our first input field is &#8220;Your name&#8221; and would map to name. The second is &#8220;Your Orginization&#8221; and would map to company.</p><ul><li>Your Name -&gt; name</li><li>Your Organization -&gt; company</li><li>Your Email Address -&gt; email</li><li>Your Telephone Number -&gt; phoneNumber</li><li>Your Website -&gt; website</li><li>Message -&gt; message</li></ul><p>Now that we&#8217;ve defined the basic syntax for mapping your cform fields to make them work with WP Highrise Contact then we get to the code modifications that must be done. When you&#8217;ve installed the WP Highrise Contact plugin open up the file named <em>wp-highrise-contact.php</em> and look for a code block that looks like the following:</p><pre class="brush:php">if (!empty($_POST['honeytrap'])) {
			/**
			 * Send notification email
			 *
			 * By default, this is commented out. Simply remove the comments to enable it and reeive a
			 * notifiation when a robot fills the form. Yes, I know, it defeats the purpose, but it's only
			 * if you want to see how the Honey Pot spam control is effective <img src='http://assets.teckpert.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />
			 */
			/*$to = get_option('wphc_email_address');
			$subject = 'SPAM - ' . get_option('wphc_email_subject');

			$body = print_r($_POST, true);
			$body .= "\r\n";
			$body .= 'Form: ' . get_permalink() . "\r\n";

			wp_mail($to, $subject, $body);
			*/
			header('location: ' . get_bloginfo('url'));
                         die;
}</pre><p>Right after this conditional block we&#8217;ll add the following loop to sort out the Cforms data that actually contains data:</p><pre class="brush:php">        /** Convert post fields to our highrise compatible entries **/
        $cforms = array();
        foreach($_POST as $k =&gt; $value)
        {
            if(strstr($k, 'field_'))
            {
                $cforms[] = $value;
            }
        }</pre><p>Because of the way we&#8217;ve added the field in Cforms the hidden field containing our mapping is the very last, from here it&#8217;s a simple matter of association so we&#8217;ll add this right after the above block.</p><pre class="brush:php">        $cfCount = count($cforms);
        $mapping = explode('^', $cforms[$cfCount - 1]);
        for($i = 0; $i &lt; $cfCount - 1; ++$i)
        {
            ${$mapping[$i]} = filter_var($cforms[$i], FILTER_SANITIZE_SPECIAL_CHARS);
        }</pre><p>Via this process we&#8217;ve made some lines redundant, so we can remove this. This the block we don&#8217;t need anymore.</p><pre class="brush:php">		foreach ( array_keys( $_POST) as $k ) {
			$$k = filter_input( INPUT_POST, $k, FILTER_SANITIZE_SPECIAL_CHARS, FILTER_FLAG_STRIP_HIGH | FILTER_FLAG_STRIP_LOW );
		}</pre><p>In our forms we normally only specify one field for the form and Highrise has a firstName and lastName field so we need to split up the name and feed it accordingly. Look for this block:</p><pre class="brush:php">if ($highrise_connected) {
			$contact = new inbox_highrise_Contact();</pre><p>Immediately afterwards we&#8217;ll insert this block of code:</p><pre class="brush:php">            if($name)
            {
                $namePieces = strstr($name, ' ') ? explode(' ', $name) : array($name, '');
                $firstName  = $namePieces[0];
                $lastName   = $namePieces[1];
            }</pre><p>What we&#8217;ve just done is use a ternary operation to check if we have two word blocks, presumably your first and last name. If not, we&#8217;ll assume we only have a first name. Then we&#8217;ll assign them to the variables the plugin uses normally.</p><p>The only thing left to do is stop the plugin from replacing the quicktag with the form since we&#8217;ve let Cforms handle the form generation, look for these two blocks:</p><pre class="brush:php">if (file_exists(TEMPLATEPATH . '/plugins/wp-highrise-contact/form.inc.php'))
    		$form = include_once(TEMPLATEPATH . '/plugins/wp-highrise-contact/form.inc.php');
    	else
        	$form = include_once(dirname( __FILE__ ) . '/form.inc.php');
        $content = str_replace('[wp-highrise-contact]', $form, $content);</pre><p>replace this one with:</p><pre class="brush:php">$content = str_replace('[wp-highrise-contact]', '', $content);</pre><pre class="brush:php">if (file_exists(TEMPLATEPATH . '/plugins/wp-highrise-contact/form.inc.php'))
    		$form = include_once(TEMPLATEPATH . '/plugins/wp-highrise-contact/form.inc.php');
    	else
        	$form = include_once(dirname( __FILE__ ) . '/form.inc.php');
        return str_replace('[wp-highrise-contact]', $form, $content)</pre><p>replace this one with:</p><pre class="brush:php">return str_replace('[wp-highrise-contact]', '', $content);</pre>]]></content:encoded> <wfw:commentRss>http://www.teckpert.com/api/highrise-cforms-ii-a-story-of-integration/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>DOM manipulation with PHP, the ultimate page scraper?</title><link>http://www.teckpert.com/php/dom-manipulation-with-php-the-ultimate-page-scraper/</link> <comments>http://www.teckpert.com/php/dom-manipulation-with-php-the-ultimate-page-scraper/#comments</comments> <pubDate>Tue, 12 Jan 2010 13:21:10 +0000</pubDate> <dc:creator>Andres</dc:creator> <category><![CDATA[Blog]]></category> <category><![CDATA[Coding Tips]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[DOM]]></category> <category><![CDATA[dom manipulation]]></category> <category><![CDATA[domdocument]]></category> <category><![CDATA[html scrapping]]></category> <category><![CDATA[javascript]]></category> <category><![CDATA[regular expression]]></category> <category><![CDATA[xPath]]></category> <category><![CDATA[xQuery]]></category> <category><![CDATA[XSLT]]></category><guid isPermaLink="false">http://www.teckpert.com/?p=395</guid> <description><![CDATA[These days we hear a good deal about DOM manipulation with JavaScript but some little known technologies ( for now - they're quickly gaining ground ) are xPath, xQuery &#038; XSLT. Fellow developers will know that historically we've had to rely on a number of regular expressions to scrape a page and while this can most often be fast, it's sometimes horrendous to read and edit as a TINY mis-write can effectively render the Regular Expression useless. That's not to say it's not useful when the hierarchy is small and simple but in today's world of web 2.0 designs they're often not.]]></description> <content:encoded><![CDATA[<p>These days we hear a good deal about DOM manipulation with JavaScript but some little known technologies ( for now &#8211; they&#8217;re quickly gaining ground ) are xPath, xQuery &amp; XSLT.</p><p>Fellow developers will know that historically we&#8217;ve had to rely on a number of regular expressions to scrape a page and while this can most often be fast, it&#8217;s sometimes horrendous to read and edit as a TINY mis-write can effectively render the Regular Expression useless. That&#8217;s not to say it&#8217;s not useful when the hierarchy is small and simple but in today&#8217;s world of web 2.0 designs they&#8217;re often not.</p><p><strong>What is page scraping?</strong></p><p>Page scraping is a method that allows you to pull information from a web page, so that the data can be manipulated inside your own script. In your script, you can connect to another URL and request a page, just like a browser would do it.  Once you make the request, the web server will send back the page you asked for and your script can manipulate the data and extract specific information.</p><p><strong>What exactly does the DomDocument Object do?</strong></p><p>If you&#8217;re not familiar with the DOM model than the following explanation probably isn&#8217;t going to make much sense as there really isn&#8217;t too much to say besides, DomDocument transform&#8217;s an HTML page into a tree-model of elements.  JavaScript does this upon load and is the entire point to the language: Dom Manipulation.  I find a visual representation often helps to understand the tree model so here is a simplified version of the aforementioned model present in DOM:</p><p style="text-align: center; "><img class="size-medium wp-image-426 aligncenter" title="dom" src="http://assets.teckpert.com/wp-content/uploads/2009/12/dom-300x251.jpg" alt="dom" width="300" height="251" /></p><p>The above is a representation of the following HTML:</p><pre class="brush:html">&lt;div&gt;
    &lt;ul&gt;
        &lt;li&gt;
            &lt;a href="#"&gt;URL&lt;/a&gt;
        &lt;/li&gt;
        &lt;li&gt;
            &lt;a href="#"&gt;&lt;img src="#" /&gt;&lt;/a&gt;
        &lt;/li&gt;
        &lt;li&gt;
            &lt;a href="#"&gt;URL&lt;/a&gt;
        &lt;/li&gt;
    &lt;ul&gt;
&lt;/div&gt;</pre><p><strong>Sidenote</strong>: If your development background is based on the more traditional languages this will look familiar to you as resembles the Binary Tree, how ever, DOM supports a limitless number of children per node.</p><p>Through the DomDocument object we are given to the ability to traverse the nodes, create them, and remove them as we see fit. However sometimes traversing the levels of DOM solely through the methods provided to us by the object is cumbersome and altogether impractical given the depth of the information that we sometimes need. Enter xPath; it is to XML compliant mark up languages what SQL is to databases, a query language. The entire breadth of xPath is outside the scope of this particular post but is covered in depth <a href="http://en.wikipedia.org/wiki/XPath_2.0" target="_blank">here</a>. If you&#8217;re familiar with jQuery or any other JavaScript framework which supports CSS style selectors this&#8217;ll be easy for you.</p><p><strong>How to use xPath with DomDocument</strong></p><p>We&#8217;ll start off with something basic as an introduction, we&#8217;ll scrape the &#8220;Why us&#8221; section of TECKpert&#8221;s homepage.</p><pre class="brush:php">&lt;?php

// Define our URL &amp; Start Dom Document
$url = 'http://www.teckpert.com';
$doc = new DOMDocument;

// Load the html into our object
$doc-&gt;loadHTMLFile($url);

// Alternatively this works too
$html = file_get_contents($url);
$doc-&gt;loadHTML($html);

// Now that we've created our dom object proper
// call the xPath object
$xPath = new DOMXPath( $doc );

// Query TECKpert's dom for the 'why us' section
$results = $xPath-&gt;query('//div[@class="why_us"]');

echo $results-&gt;item(0)-&gt;textContent;</pre><p>Simple to use right? There will be more to come on the onset of technologies associates with XML traversing and it&#8217;s related query languages.</p><p><strong>Note on this article</strong><br /> There exists the possibility of violating copyright laws using techniques outlined in this article if you misuse data you scrape. Please scrape responsibly.</p> ]]></content:encoded> <wfw:commentRss>http://www.teckpert.com/php/dom-manipulation-with-php-the-ultimate-page-scraper/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> </channel> </rss>
<!-- Served from: www.teckpert.com @ 2012-02-06 01:02:41 by W3 Total Cache -->
