<?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>PHP - Devhour</title>
	<atom:link href="https://www.devhour.net/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.devhour.net</link>
	<description>Taking time to write about development</description>
	<lastBuildDate>Tue, 12 Mar 2024 10:01:42 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.5.2</generator>

<image>
	<url>https://www.devhour.net/wp-content/uploads/2024/03/cropped-devhourlogo-32x32.png</url>
	<title>PHP - Devhour</title>
	<link>https://www.devhour.net</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Reading a websites contents using PHP and XPath</title>
		<link>https://www.devhour.net/reading-a-websites-contents-using-php-and-xpath/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=reading-a-websites-contents-using-php-and-xpath</link>
					<comments>https://www.devhour.net/reading-a-websites-contents-using-php-and-xpath/#respond</comments>
		
		<dc:creator><![CDATA[Jamie]]></dc:creator>
		<pubDate>Mon, 16 Sep 2013 12:32:00 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Dev Hour]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[XPath]]></category>
		<guid isPermaLink="false">https://www.devhour.net/?p=32</guid>

					<description><![CDATA[<p>Following on from my previous post Scraping data with PHP and cURL today I’m going to show you about reading a websites contents using PHP and XPath. One of the biggest problems I have faced when dealing with a scraped web page (read: block of HTML) is how to go about getting specific text elements out of it. Several years [&#8230;]</p>
<p>The post <a href="https://www.devhour.net/reading-a-websites-contents-using-php-and-xpath/">Reading a websites contents using PHP and XPath</a> first appeared on <a href="https://www.devhour.net">Devhour</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Following on from my previous post <a href="https://www.devhour.net/filling-out-forms-with-php-and-curl/">Scraping data with PHP and cURL</a> today I’m going to show you about reading a websites contents using <a href="https://php.net/" target="_blank" rel="noreferrer noopener">PHP</a> and <a href="https://www.w3schools.com/xpath/xpath_syntax.asp" target="_blank" rel="noreferrer noopener">XPath</a>.</p>



<p>One of the biggest problems I have faced when dealing with a scraped web page (read: block of HTML) is how to go about getting specific text elements out of it. Several years ago I would’ve gone with regex. This was a hassle to me and I’ve still yet to get my head around how it correctly works. These days though, I have discovered a useful little thing called XPath.</p>



<p>Basically, how it works is, you put the HTML in a DOMDocument and then you can use XPath to navigate through elements and attributes of the document getting the data of them in the process.</p>



<h2 class="wp-block-heading">Using PHP and XPath</h2>



<p>Now if we were to consider the following HTML:</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#575279;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#faf4ed"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="&lt;html&gt;   
  &lt;head&gt;
    &lt;title&gt;Test&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;div id=&quot;mainDiv&quot;&gt;
      &lt;div id=&quot;leftDiv&quot;&gt;
        &lt;p class=&quot;bodyText&quot;&gt;This is left&lt;/p&gt;
        &lt;img src=&quot;images/test.jpg&quot; /&gt;
      &lt;/div&gt;
      &lt;div id=&quot;rightDiv&quot;&gt;
        &lt;p class=&quot;bodyText&quot;&gt;This is right&lt;/p&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div id=&quot;footer&quot;&gt;
      &lt;p class=&quot;smallText&quot;&gt;This is the footer&lt;/p&gt;
    &lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;" style="color:#575279;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #9893A5">&lt;</span><span style="color: #56949F">html</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">   </span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">head</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">title</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">Test</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">title</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">head</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">body</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">div</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">id</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;mainDiv&quot;</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">      </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">div</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">id</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;leftDiv&quot;</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">p</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">class</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;bodyText&quot;</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">This is left</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">img</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">src</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;images/test.jpg&quot;</span><span style="color: #575279"> </span><span style="color: #9893A5">/&gt;</span></span>
<span class="line"><span style="color: #575279">      </span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">div</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">      </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">div</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">id</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;rightDiv&quot;</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">p</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">class</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;bodyText&quot;</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">This is right</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">      </span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">div</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">div</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">div</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">id</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;footer&quot;</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">      </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">p</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">class</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;smallText&quot;</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">This is the footer</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">div</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">body</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">html</span><span style="color: #9893A5">&gt;</span></span></code></pre><span style="display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#faf4ed;color:#625c88;font-size:12px;line-height:1;position:relative">HTML</span></div>



<p>And now, lets say I wanted to get the text of the rightDiv. This is how we would go about it:</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#575279;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#faf4ed"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="&lt;?php
  // $content is the content you scraped (via curl for example)
  $dom = new DOMDocument();
  @$dom-&gt;loadHTML($content)
  $xpath = new DOMXPath($dom);
  $rightDivText = $xpath-&gt;query(&quot;//html/body/div[@id='mainDiv']/div[@id='rightDiv']/p/text()&quot;); 
  
  // Returns: This is right
  echo $rightDivText-&gt;item(0)-&gt;nodeValue;
?&gt;" style="color:#575279;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #286983">&lt;?php</span></span>
<span class="line"><span style="color: #797593">  </span><span style="color: #797593; font-style: italic">//</span><span style="color: #9893A5; font-style: italic"> $content is the content you scraped (via curl for example)</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">dom</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #286983">new</span><span style="color: #575279"> </span><span style="color: #56949F">DOMDocument</span><span style="color: #797593">();</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #286983">@</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">dom</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">loadHTML</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">content</span><span style="color: #797593">)</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">xpath</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #286983">new</span><span style="color: #575279"> </span><span style="color: #56949F">DOMXPath</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">dom</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">rightDivText</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">xpath</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">query</span><span style="color: #797593">(</span><span style="color: #EA9D34">&quot;//html/body/div[@id=&#39;mainDiv&#39;]/div[@id=&#39;rightDiv&#39;]/p/text()&quot;</span><span style="color: #797593">);</span><span style="color: #575279"> </span></span>
<span class="line"><span style="color: #575279">  </span></span>
<span class="line"><span style="color: #797593">  </span><span style="color: #797593; font-style: italic">//</span><span style="color: #9893A5; font-style: italic"> Returns: This is right</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #B4637A; font-style: italic">echo</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">rightDivText</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">item</span><span style="color: #797593">(</span><span style="color: #D7827E">0</span><span style="color: #797593">)</span><span style="color: #286983">-&gt;</span><span style="color: #575279; font-style: italic">nodeValue</span><span style="color: #797593">;</span></span>
<span class="line"><span style="color: #286983">?&gt;</span></span></code></pre><span style="display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#faf4ed;color:#625c88;font-size:12px;line-height:1;position:relative">PHP</span></div>



<p>Simply, it will load the $content (HTML) into a DOMDocument and then using XPath it looks for the for the div with the id of ‘mainDiv’ and then inside of ‘mainDiv’ it looks for the div with the id of ‘rightDiv’. Finally it then gets the text of the ‘p’ element.</p>



<p>Now that is one of the more straight forward scenarios and is made easier by the fact that most of the different elements have custom IDs like ‘mainDiv’ or ‘rightDiv’. If we were to look at the same HTML block again but this time without IDs, this is how it would look.</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#575279;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#faf4ed"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Test&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;div&gt;
      &lt;div&gt;
        &lt;p class=&quot;bodyText&quot;&gt;This is left&lt;/p&gt;
        &lt;img src=&quot;images/test.jpg&quot; /&gt;
      &lt;/div&gt;

      &lt;div&gt;
        &lt;p class=&quot;bodyText&quot;&gt;This is right&lt;/p&gt;
      &lt;/div&gt;
    &lt;/div&gt;

    &lt;div id=&quot;footer&quot;&gt;
      &lt;p class=&quot;smallText&quot;&gt;This is the footer&lt;/p&gt;
    &lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;
" style="color:#575279;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #9893A5">&lt;</span><span style="color: #56949F">html</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">head</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">title</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">Test</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">title</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">head</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">body</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">div</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">      </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">div</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">p</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">class</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;bodyText&quot;</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">This is left</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">img</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">src</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;images/test.jpg&quot;</span><span style="color: #575279"> </span><span style="color: #9893A5">/&gt;</span></span>
<span class="line"><span style="color: #575279">      </span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">div</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #575279">      </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">div</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">p</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">class</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;bodyText&quot;</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">This is right</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">      </span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">div</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">div</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">div</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">id</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;footer&quot;</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">      </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">p</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">class</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;smallText&quot;</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">This is the footer</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">div</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">body</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">html</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"></span></code></pre><span style="display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#faf4ed;color:#625c88;font-size:12px;line-height:1;position:relative">HTML</span></div>



<p>This time lets get the src of the image in the former leftDiv.</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#575279;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#faf4ed"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="&lt;?php
// $content is the content you scraped (via curl for example)
$dom = new DOMDocument();  @$dom-&gt;loadHTML($content)
$xpath = new DOMXPath($dom);
$imgSrc = $xpath-&gt;query(&quot;//html/body/div[1]/div[1]/img/@src&quot;);

// Returns: images/test.jpg
echo $imgSrc-&gt;item(0)-&gt;nodeValue;
?&gt;
" style="color:#575279;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #286983">&lt;?php</span></span>
<span class="line"><span style="color: #797593; font-style: italic">//</span><span style="color: #9893A5; font-style: italic"> $content is the content you scraped (via curl for example)</span></span>
<span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">dom</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #286983">new</span><span style="color: #575279"> </span><span style="color: #56949F">DOMDocument</span><span style="color: #797593">();</span><span style="color: #286983">  @</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">dom</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">loadHTML</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">content</span><span style="color: #797593">)</span></span>
<span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">xpath</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #286983">new</span><span style="color: #575279"> </span><span style="color: #56949F">DOMXPath</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">dom</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">imgSrc</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">xpath</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">query</span><span style="color: #797593">(</span><span style="color: #EA9D34">&quot;//html/body/div[1]/div[1]/img/@src&quot;</span><span style="color: #797593">);</span></span>
<span class="line"></span>
<span class="line"><span style="color: #797593; font-style: italic">//</span><span style="color: #9893A5; font-style: italic"> Returns: images/test.jpg</span></span>
<span class="line"><span style="color: #B4637A; font-style: italic">echo</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">imgSrc</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">item</span><span style="color: #797593">(</span><span style="color: #D7827E">0</span><span style="color: #797593">)</span><span style="color: #286983">-&gt;</span><span style="color: #575279; font-style: italic">nodeValue</span><span style="color: #797593">;</span></span>
<span class="line"><span style="color: #286983">?&gt;</span></span>
<span class="line"></span></code></pre><span style="display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#faf4ed;color:#625c88;font-size:12px;line-height:1;position:relative">PHP</span></div>



<p>As you can see, the difference this time is that instead of using the divs ID, we used the elements number. Inside the body, it looks for the first div, and then inside of that it again looks for the first child div before returning the src of the img element.</p>



<p>Again though, this is probably another pretty simple solution but definitely gives you an idea of how XPath works and some of the ways it can be used. The next scenario is basically the one I came face to face with, and that scenario is one in which we have multiple divs with the same name. Consider this html:</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#575279;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#faf4ed"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Test&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;div id=&quot;container&quot;&gt;
      &lt;div id=&quot;person&quot;&gt;
        &lt;p&gt;Bob&lt;/p&gt;
        &lt;p&gt;25&lt;/p&gt;
        &lt;p&gt;180lbs&lt;/p&gt;
        &lt;img src=&quot;images/p1.jpg&quot; /&gt;
      &lt;/div&gt;
      &lt;div id=&quot;person&quot;&gt;
        &lt;p&gt;Stacy&lt;/p&gt;
        &lt;p&gt;26&lt;/p&gt;
        &lt;p&gt;187lbs&lt;/p&gt;
        &lt;img src=&quot;images/p2.jpg&quot; /&gt;
      &lt;/div&gt;
      &lt;div id=&quot;person&quot;&gt;
        &lt;p&gt;John&lt;/p&gt;
        &lt;p&gt;21&lt;/p&gt;
        &lt;p&gt;255lbs&lt;/p&gt;
        &lt;img src=&quot;images/p3.jpg&quot; /&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div id=&quot;footer&quot;&gt;&lt;p class=&quot;smallText&quot;&gt;This is the footer&lt;/p&gt;&lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;
" style="color:#575279;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #9893A5">&lt;</span><span style="color: #56949F">html</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">head</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">title</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">Test</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">title</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">head</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">body</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">div</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">id</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;container&quot;</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">      </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">div</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">id</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;person&quot;</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">Bob</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">25</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">180lbs</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">img</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">src</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;images/p1.jpg&quot;</span><span style="color: #575279"> </span><span style="color: #9893A5">/&gt;</span></span>
<span class="line"><span style="color: #575279">      </span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">div</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">      </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">div</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">id</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;person&quot;</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">Stacy</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">26</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">187lbs</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">img</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">src</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;images/p2.jpg&quot;</span><span style="color: #575279"> </span><span style="color: #9893A5">/&gt;</span></span>
<span class="line"><span style="color: #575279">      </span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">div</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">      </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">div</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">id</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;person&quot;</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">John</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">21</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">255lbs</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">img</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">src</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;images/p3.jpg&quot;</span><span style="color: #575279"> </span><span style="color: #9893A5">/&gt;</span></span>
<span class="line"><span style="color: #575279">      </span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">div</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">div</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">div</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">id</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;footer&quot;</span><span style="color: #9893A5">&gt;&lt;</span><span style="color: #56949F">p</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">class</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;smallText&quot;</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">This is the footer</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;&lt;/</span><span style="color: #56949F">div</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">body</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">html</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"></span></code></pre><span style="display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#faf4ed;color:#625c88;font-size:12px;line-height:1;position:relative">HTML</span></div>



<p>So here we have a webpage that displays a bunch of people with information such as their name, age, weight, and picture. Now as someone who is interested in getting the information of all of these people, I need to scrape the website and then using XPath I can loop through each person and get it.</p>



<p>To do this, we get everything inside the ‘container’ div and then loop through each individual ‘person’ div. Then using XPath on the ‘person’ div we can use the above examples to get the information we want. Finally we store all the results in an array.</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#575279;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#faf4ed"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code=" &lt;?php
$dom = new DOMDocument();
@$dom-&gt;loadHTML($content);
$tempDom   = new DOMDocument();
$xpath     = new DOMXPath($dom);
$container = $xpath-&gt;query(&quot;//div[@id='container']&quot;);
foreach ($container as $item) {
    $tempDom-&gt;appendChild($tempDom-&gt;importNode($item, true));
}
$tempDom-&gt;saveHTML();
$peopleXpath = new DOMXPath($tempDom);
$peopleDiv   = $peopleXpath-&gt;query(&quot;div[@id='person']&quot;);
$results     = array();
foreach ($peopleDiv as $people) {
    $newDom = new DOMDocument;
    $newDom-&gt;appendChild($newDom-&gt;importNode($people, true));
    $personXpath = new DOMXPath($newDom);
    $name        = trim($personXpath-&gt;query(&quot;p[1]/text()&quot;)-&gt;item(0)-&gt;nodeValue);
    $age         = trim($resultXpath-&gt;query(&quot;p[2]/text()&quot;)-&gt;item(0)-&gt;nodeValue);
    $weight      = trim($resultXpath-&gt;query(&quot;p[3]/text()&quot;)-&gt;item(0)-&gt;nodeValue);
    $image       = trim($resultXpath-&gt;query(&quot;img[1]/@src&quot;)-&gt;item(0)-&gt;nodeValue);
    $results[]   = array(
        'name' =&gt; $name,
        'age' =&gt; $age,
        'weight' =&gt; $weight,
        'image' =&gt; $image
    );
}
?&gt; " style="color:#575279;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #575279"> </span><span style="color: #286983">&lt;?php</span></span>
<span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">dom</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #286983">new</span><span style="color: #575279"> </span><span style="color: #56949F">DOMDocument</span><span style="color: #797593">();</span></span>
<span class="line"><span style="color: #286983">@</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">dom</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">loadHTML</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">content</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">tempDom</span><span style="color: #575279">   </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #286983">new</span><span style="color: #575279"> </span><span style="color: #56949F">DOMDocument</span><span style="color: #797593">();</span></span>
<span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">xpath</span><span style="color: #575279">     </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #286983">new</span><span style="color: #575279"> </span><span style="color: #56949F">DOMXPath</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">dom</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">container</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">xpath</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">query</span><span style="color: #797593">(</span><span style="color: #EA9D34">&quot;//div[@id=&#39;container&#39;]&quot;</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #286983">foreach</span><span style="color: #575279"> </span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">container</span><span style="color: #575279"> </span><span style="color: #286983">as</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">item</span><span style="color: #797593">)</span><span style="color: #575279"> </span><span style="color: #797593">{</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">tempDom</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">appendChild</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">tempDom</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">importNode</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">item</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #D7827E">true</span><span style="color: #797593">));</span></span>
<span class="line"><span style="color: #797593">}</span></span>
<span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">tempDom</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">saveHTML</span><span style="color: #797593">();</span></span>
<span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">peopleXpath</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #286983">new</span><span style="color: #575279"> </span><span style="color: #56949F">DOMXPath</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">tempDom</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">peopleDiv</span><span style="color: #575279">   </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">peopleXpath</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">query</span><span style="color: #797593">(</span><span style="color: #EA9D34">&quot;div[@id=&#39;person&#39;]&quot;</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">results</span><span style="color: #575279">     </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #B4637A; font-style: italic">array</span><span style="color: #797593">();</span></span>
<span class="line"><span style="color: #286983">foreach</span><span style="color: #575279"> </span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">peopleDiv</span><span style="color: #575279"> </span><span style="color: #286983">as</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">people</span><span style="color: #797593">)</span><span style="color: #575279"> </span><span style="color: #797593">{</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">newDom</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #286983">new</span><span style="color: #575279"> </span><span style="color: #56949F">DOMDocument</span><span style="color: #797593">;</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">newDom</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">appendChild</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">newDom</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">importNode</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">people</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #D7827E">true</span><span style="color: #797593">));</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">personXpath</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #286983">new</span><span style="color: #575279"> </span><span style="color: #56949F">DOMXPath</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">newDom</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">name</span><span style="color: #575279">        </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #B4637A; font-style: italic">trim</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">personXpath</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">query</span><span style="color: #797593">(</span><span style="color: #EA9D34">&quot;p[1]/text()&quot;</span><span style="color: #797593">)</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">item</span><span style="color: #797593">(</span><span style="color: #D7827E">0</span><span style="color: #797593">)</span><span style="color: #286983">-&gt;</span><span style="color: #575279; font-style: italic">nodeValue</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">age</span><span style="color: #575279">         </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #B4637A; font-style: italic">trim</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">resultXpath</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">query</span><span style="color: #797593">(</span><span style="color: #EA9D34">&quot;p[2]/text()&quot;</span><span style="color: #797593">)</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">item</span><span style="color: #797593">(</span><span style="color: #D7827E">0</span><span style="color: #797593">)</span><span style="color: #286983">-&gt;</span><span style="color: #575279; font-style: italic">nodeValue</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">weight</span><span style="color: #575279">      </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #B4637A; font-style: italic">trim</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">resultXpath</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">query</span><span style="color: #797593">(</span><span style="color: #EA9D34">&quot;p[3]/text()&quot;</span><span style="color: #797593">)</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">item</span><span style="color: #797593">(</span><span style="color: #D7827E">0</span><span style="color: #797593">)</span><span style="color: #286983">-&gt;</span><span style="color: #575279; font-style: italic">nodeValue</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">image</span><span style="color: #575279">       </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #B4637A; font-style: italic">trim</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">resultXpath</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">query</span><span style="color: #797593">(</span><span style="color: #EA9D34">&quot;img[1]/@src&quot;</span><span style="color: #797593">)</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">item</span><span style="color: #797593">(</span><span style="color: #D7827E">0</span><span style="color: #797593">)</span><span style="color: #286983">-&gt;</span><span style="color: #575279; font-style: italic">nodeValue</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">results</span><span style="color: #797593">[]</span><span style="color: #575279">   </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #B4637A; font-style: italic">array</span><span style="color: #797593">(</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #EA9D34">&#39;name&#39;</span><span style="color: #575279"> </span><span style="color: #286983">=&gt;</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">name</span><span style="color: #797593">,</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #EA9D34">&#39;age&#39;</span><span style="color: #575279"> </span><span style="color: #286983">=&gt;</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">age</span><span style="color: #797593">,</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #EA9D34">&#39;weight&#39;</span><span style="color: #575279"> </span><span style="color: #286983">=&gt;</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">weight</span><span style="color: #797593">,</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #EA9D34">&#39;image&#39;</span><span style="color: #575279"> </span><span style="color: #286983">=&gt;</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">image</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #797593">}</span></span>
<span class="line"><span style="color: #286983">?&gt;</span><span style="color: #575279"> </span></span></code></pre><span style="display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#faf4ed;color:#625c88;font-size:12px;line-height:1;position:relative">PHP</span></div>



<p>There you have it. If you run a print_r on the results array you should have yourself a bunch values containing the information of Bob, Stacy, and John.</p>



<p>Now a totally awesome cheat for this solution is to use the Google Chrome <a href="https://chrome.google.com/webstore/detail/xpath-helper/hgimnogjllphhhkhlmebbmlgjoejdpjl?hl=en" target="_blank" rel="noreferrer noopener">XPath Helper</a> extension. All you need to do is hold down the shift key as you hover over webpage elements. This in turn will return the XPath query which is a HUGE help!</p>



<p>As always feel free to comment and or improve on the above. Also, don’t forget to follow me <a href="https://twitter.com/JAGracie" target="_blank" rel="noreferrer noopener">@JAGracie</a>.</p><p>The post <a href="https://www.devhour.net/reading-a-websites-contents-using-php-and-xpath/">Reading a websites contents using PHP and XPath</a> first appeared on <a href="https://www.devhour.net">Devhour</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://www.devhour.net/reading-a-websites-contents-using-php-and-xpath/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Automatically post to a Page using PHP Facebook SDK</title>
		<link>https://www.devhour.net/automatically-post-to-a-page-using-php-facebook-sdk/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=automatically-post-to-a-page-using-php-facebook-sdk</link>
					<comments>https://www.devhour.net/automatically-post-to-a-page-using-php-facebook-sdk/#respond</comments>
		
		<dc:creator><![CDATA[Jamie]]></dc:creator>
		<pubDate>Tue, 10 Sep 2013 12:30:00 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<guid isPermaLink="false">https://www.devhour.net/?p=30</guid>

					<description><![CDATA[<p>During my weekends/time off I tend to watch quite a lot of sport, with the English Premier League (EPL) being one of my favourite things to support. As such, I created and run an EPL facebook page dedicated  to covering fixtures, results, and other news. Now this all good fun and I enjoy doing it, but week after [&#8230;]</p>
<p>The post <a href="https://www.devhour.net/automatically-post-to-a-page-using-php-facebook-sdk/">Automatically post to a Page using PHP Facebook SDK</a> first appeared on <a href="https://www.devhour.net">Devhour</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>During my weekends/time off I tend to watch quite a lot of sport, with the English Premier League (EPL) being one of my favourite things to support. As such, I created and run an <a href="https://www.facebook.com/EPLNewsInfo" target="_blank" rel="noreferrer noopener">EPL facebook page</a> dedicated  to covering fixtures, results, and other news.</p>



<p>Now this all good fun and I enjoy doing it, but week after week, month after month of posting all the fixtures the day before and posting the results the day after gets a little tedious, especially if I’m short on time. So being the geek I am, I decided that I could easily automate these, and so I did, using the PHP facebook SDK.</p>



<p>It was actually pretty straight forward, the hardest bit was trying to find websites which would give me the information in a straight forward and easy way. In the end, I decided to go with the offical <a href="https://www.premierleague.com/en-gb/matchday/results.html?paramComp_8=true&amp;view=.dateSeason" target="_blank" rel="noreferrer noopener">Premier League website</a>, as not only is it the correct information it is also laid out in a way which makes it easy to retrieve.</p>



<p>So putting my previous posts into good use I used curl to scrape the website and Xpath to dissect it into the important parts (namely the home and away teams, the location, and the final score).</p>



<p>Once I have this important information I can then go away and use the PHP facebook SDK to post a status to my EPL News feed which I will show you how to do now.</p>



<p>Firstly, you will need to create a <a href="https://developers.facebook.com/apps" target="_blank" rel="noreferrer noopener">facebook application</a> which will be integrated in a website. After you’ve created the app you will want to fill out the website URL and hit save. This URL is going to be where the PHP app you are going to create will be stored (ie http://devhour.net/fbapp).</p>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="707" height="434" src="https://www.devhour.net/wp-content/uploads/2024/03/AutomaticallyPosttoPHPFacebook-EPLNewsFeed.png" alt="EPLNewsFeed Facebook Application" class="wp-image-58" srcset="https://www.devhour.net/wp-content/uploads/2024/03/AutomaticallyPosttoPHPFacebook-EPLNewsFeed.png 707w, https://www.devhour.net/wp-content/uploads/2024/03/AutomaticallyPosttoPHPFacebook-EPLNewsFeed-300x184.png 300w" sizes="(max-width: 707px) 100vw, 707px" /><figcaption class="wp-element-caption">EPLNewsFeed Facebook Application</figcaption></figure>



<p>Next up you’ll need to run the following URL to authorize your app to manage your pages and other such permissions. Remember to replace [APP_ID] and [SITE_URL] with both your facebook APP ID and the website you entered above. You will be prompted to grant access to your app (click Allow).</p>



<pre class="wp-block-code"><code>https:&#47;&#47;graph.facebook.com/oauth/authorize?client_id=&#91;APP_ID]&amp;scope=offline_access,publish_stream,manage_pages&amp;redirect_uri=&#91;SITE_URL]</code></pre>



<p>It will then redirect you back to the URL of the site you specified in the app. If you look closely though you’ll notice that on the end of the url is ?code=aBuNcHoFgIbBeRiSh. Copy everything after the ?code= and keep it in a safe place for now. You’re going to need it in a second.</p>



<p>The next step is to get the long lasting access token which will allow you to keep posting without having to authenticate yourself every time (this is vital to making this whole script automatic). So again go to the following URL and replace [APP_ID], [APP_SECRET], [SITE_URL], and [CODE] (the one you just previously copied).</p>



<pre class="wp-block-code"><code>https:&#47;&#47;graph.facebook.com/oauth/access_token?client_id=&#91;APP_ID]&amp;redirect_uri=&#91;SITE_URL]&amp;client_secret=&#91;APP_SECRET]&amp;code=&#91;CODE]</code></pre>



<p>If all went well you’ll be redirected back to your website and on the screen you should see:</p>



<p>access_token=…….</p>



<p>Store this somewhere because it’s very important <img src="https://s.w.org/images/core/emoji/15.0.3/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>Now the fun can start. Firstly, you can test the access token by going to the <a href="https://developers.facebook.com/tools/explorer/" target="_blank" rel="noreferrer noopener">Facebook Graph API explorer</a>, pasting the token into the Access token field and trying the following:</p>



<figure class="wp-block-image size-full"><img decoding="async" width="925" height="213" src="https://www.devhour.net/wp-content/uploads/2024/03/AutomaticallyPosttoPHPFacebook-GraphAPI.png" alt="Facebook Graph API Explorer" class="wp-image-60" srcset="https://www.devhour.net/wp-content/uploads/2024/03/AutomaticallyPosttoPHPFacebook-GraphAPI.png 925w, https://www.devhour.net/wp-content/uploads/2024/03/AutomaticallyPosttoPHPFacebook-GraphAPI-300x69.png 300w, https://www.devhour.net/wp-content/uploads/2024/03/AutomaticallyPosttoPHPFacebook-GraphAPI-768x177.png 768w" sizes="(max-width: 925px) 100vw, 925px" /><figcaption class="wp-element-caption">Facebook Graph API Explorer</figcaption></figure>



<p>This will post a test message to your page which will indicate that the access token you have is good to go. Now on to the code…</p>



<p>Before we write any code on our end, you’re going to have to download the <a href="https://github.com/facebook/facebook-php-sdk" target="_blank" rel="noreferrer noopener">PHP facebook SDK</a> from the github repo. Download and store these files somewhere handy and feel free to jump into the examples are have a bit of a play.</p>



<p>On our end however, the first thing we are going to is create a php file and reference the facebook sdk and enter some variable details (fill out as necessary).</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#575279;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#faf4ed"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code=" &lt;?php
require &quot;src/facebook.php&quot;;
$PAGE_ID         = &quot;&quot;; // The page you want to post to (you must be a manager) 
$FACEBOOK_APP_ID = &quot;&quot;; // Your facebook app ID
$FACEBOOK_SECRET = &quot;&quot;; // Your facebook secret
$ACCESS_TOKEN    = &quot;&quot;; // The access token you receieved above
$PAGE_TOKEN      = &quot;&quot;; // Leave this blank. It will be set later " style="color:#575279;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #575279"> </span><span style="color: #286983">&lt;?php</span></span>
<span class="line"><span style="color: #286983">require</span><span style="color: #575279"> </span><span style="color: #EA9D34">&quot;src/facebook.php&quot;</span><span style="color: #797593">;</span></span>
<span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">PAGE_ID</span><span style="color: #575279">         </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #EA9D34">&quot;&quot;</span><span style="color: #797593">;</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">//</span><span style="color: #9893A5; font-style: italic"> The page you want to post to (you must be a manager) </span></span>
<span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">FACEBOOK_APP_ID</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #EA9D34">&quot;&quot;</span><span style="color: #797593">;</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">//</span><span style="color: #9893A5; font-style: italic"> Your facebook app ID</span></span>
<span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">FACEBOOK_SECRET</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #EA9D34">&quot;&quot;</span><span style="color: #797593">;</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">//</span><span style="color: #9893A5; font-style: italic"> Your facebook secret</span></span>
<span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">ACCESS_TOKEN</span><span style="color: #575279">    </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #EA9D34">&quot;&quot;</span><span style="color: #797593">;</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">//</span><span style="color: #9893A5; font-style: italic"> The access token you receieved above</span></span>
<span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">PAGE_TOKEN</span><span style="color: #575279">      </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #EA9D34">&quot;&quot;</span><span style="color: #797593">;</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">//</span><span style="color: #9893A5; font-style: italic"> Leave this blank. It will be set later </span></span></code></pre><span style="display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#faf4ed;color:#625c88;font-size:12px;line-height:1;position:relative">PHP</span></div>



<p>Now we go ahead and create a new facebook instance using our newly created app details.</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#575279;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#faf4ed"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="$facebook = new Facebook(array('appId' =&gt; $FACEBOOK_APP_ID, 'secret' =&gt; $FACEBOOK_SECRET, 'cookie' =&gt; true,));  
$post = array('access_token' =&gt; $ACCESS_TOKEN);" style="color:#575279;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">facebook</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #286983">new</span><span style="color: #575279"> </span><span style="color: #56949F">Facebook</span><span style="color: #797593">(</span><span style="color: #B4637A; font-style: italic">array</span><span style="color: #797593">(</span><span style="color: #EA9D34">&#39;appId&#39;</span><span style="color: #575279"> </span><span style="color: #286983">=&gt;</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">FACEBOOK_APP_ID</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #EA9D34">&#39;secret&#39;</span><span style="color: #575279"> </span><span style="color: #286983">=&gt;</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">FACEBOOK_SECRET</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #EA9D34">&#39;cookie&#39;</span><span style="color: #575279"> </span><span style="color: #286983">=&gt;</span><span style="color: #575279"> </span><span style="color: #D7827E">true</span><span style="color: #797593">,));</span><span style="color: #575279">  </span></span>
<span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">post</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #B4637A; font-style: italic">array</span><span style="color: #797593">(</span><span style="color: #EA9D34">&#39;access_token&#39;</span><span style="color: #575279"> </span><span style="color: #286983">=&gt;</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">ACCESS_TOKEN</span><span style="color: #797593">);</span></span></code></pre><span style="display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#faf4ed;color:#625c88;font-size:12px;line-height:1;position:relative">PHP</span></div>



<p>The next little piece of code will loop through the pages that you manage and find the PAGE_TOKEN which matches the PAGE_ID you specified. This will allow you to post to that pages wall as the page rather than you.</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#575279;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#faf4ed"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="try {  
  $res = $facebook-&gt;api('/me/accounts','GET',$post);   
  if (isset($res['data'])) {
    foreach ($res['data'] as $account) {
      if ($PAGE_ID == $account['id']) { 
        $PAGE_TOKEN = $account['access_token']; 
        break; 
      }
    }
  }
} 
catch (Exception $e){  
 echo $e-&gt;getMessage();
}" style="color:#575279;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #286983">try</span><span style="color: #575279"> </span><span style="color: #797593">{</span><span style="color: #286983">  </span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">res</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">facebook</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">api</span><span style="color: #797593">(</span><span style="color: #EA9D34">&#39;/me/accounts&#39;</span><span style="color: #797593">,</span><span style="color: #EA9D34">&#39;GET&#39;</span><span style="color: #797593">,</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">post</span><span style="color: #797593">);</span><span style="color: #286983">  </span><span style="color: #575279"> </span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #286983">if</span><span style="color: #575279"> </span><span style="color: #797593">(</span><span style="color: #B4637A; font-style: italic">isset</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">res</span><span style="color: #797593">[</span><span style="color: #EA9D34">&#39;data&#39;</span><span style="color: #797593">]))</span><span style="color: #575279"> </span><span style="color: #797593">{</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #286983">foreach</span><span style="color: #575279"> </span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">res</span><span style="color: #797593">[</span><span style="color: #EA9D34">&#39;data&#39;</span><span style="color: #797593">]</span><span style="color: #575279"> </span><span style="color: #286983">as</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">account</span><span style="color: #797593">)</span><span style="color: #575279"> </span><span style="color: #797593">{</span></span>
<span class="line"><span style="color: #575279">      </span><span style="color: #286983">if</span><span style="color: #575279"> </span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">PAGE_ID</span><span style="color: #575279"> </span><span style="color: #286983">==</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">account</span><span style="color: #797593">[</span><span style="color: #EA9D34">&#39;id&#39;</span><span style="color: #797593">])</span><span style="color: #575279"> </span><span style="color: #797593">{</span><span style="color: #286983"> </span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">PAGE_TOKEN</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">account</span><span style="color: #797593">[</span><span style="color: #EA9D34">&#39;access_token&#39;</span><span style="color: #797593">];</span><span style="color: #286983"> </span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #286983">break</span><span style="color: #797593">;</span><span style="color: #286983"> </span></span>
<span class="line"><span style="color: #575279">      </span><span style="color: #797593">}</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #797593">}</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #797593">}</span></span>
<span class="line"><span style="color: #797593">}</span><span style="color: #575279"> </span></span>
<span class="line"><span style="color: #286983">catch</span><span style="color: #575279"> </span><span style="color: #797593">(</span><span style="color: #56949F">Exception</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">e</span><span style="color: #797593">){</span><span style="color: #286983">  </span></span>
<span class="line"><span style="color: #575279"> </span><span style="color: #B4637A; font-style: italic">echo</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">e</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">getMessage</span><span style="color: #797593">();</span></span>
<span class="line"><span style="color: #797593">}</span></span></code></pre><span style="display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#faf4ed;color:#625c88;font-size:12px;line-height:1;position:relative">PHP</span></div>



<p>Taking all the information we have so far received we can now go ahead and post to the pages wall.</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#575279;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#faf4ed"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="$message = &quot;This is a test message&quot;;
$post = array('access_token' =&gt; $PAGE_TOKEN, 'message' =&gt; $message);
try{
  $res = $facebook-&gt;api(&quot;/$PAGE_ID/feed&quot;,&quot;POST&quot;,$post);
}
catch (Exception $e){
  echo $e-&gt;getMessage();
}
?&gt;" style="color:#575279;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">message</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #EA9D34">&quot;This is a test message&quot;</span><span style="color: #797593">;</span></span>
<span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">post</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #B4637A; font-style: italic">array</span><span style="color: #797593">(</span><span style="color: #EA9D34">&#39;access_token&#39;</span><span style="color: #575279"> </span><span style="color: #286983">=&gt;</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">PAGE_TOKEN</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #EA9D34">&#39;message&#39;</span><span style="color: #575279"> </span><span style="color: #286983">=&gt;</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">message</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #286983">try</span><span style="color: #797593">{</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">res</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">facebook</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">api</span><span style="color: #797593">(</span><span style="color: #EA9D34">&quot;/</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">PAGE_ID</span><span style="color: #EA9D34">/feed&quot;</span><span style="color: #797593">,</span><span style="color: #EA9D34">&quot;POST&quot;</span><span style="color: #797593">,</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">post</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #797593">}</span></span>
<span class="line"><span style="color: #286983">catch</span><span style="color: #575279"> </span><span style="color: #797593">(</span><span style="color: #56949F">Exception</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">e</span><span style="color: #797593">){</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #B4637A; font-style: italic">echo</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">e</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">getMessage</span><span style="color: #797593">();</span></span>
<span class="line"><span style="color: #797593">}</span></span>
<span class="line"><span style="color: #286983">?&gt;</span></span></code></pre><span style="display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#faf4ed;color:#625c88;font-size:12px;line-height:1;position:relative">PHP</span></div>



<p>And that’s it. It’s actually really straight forward and easy to do. Feel free to play around with it and I’d be interested in seeing what you can come up with.</p>



<p>As always, follow me on twitter: <a href="https://twitter.com/JAGracie" target="_blank" rel="noreferrer noopener">@JAGracie</a></p><p>The post <a href="https://www.devhour.net/automatically-post-to-a-page-using-php-facebook-sdk/">Automatically post to a Page using PHP Facebook SDK</a> first appeared on <a href="https://www.devhour.net">Devhour</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://www.devhour.net/automatically-post-to-a-page-using-php-facebook-sdk/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Sending messages to an Eggdrop via PHP</title>
		<link>https://www.devhour.net/sending-messages-to-an-eggdrop-via-php/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=sending-messages-to-an-eggdrop-via-php</link>
					<comments>https://www.devhour.net/sending-messages-to-an-eggdrop-via-php/#respond</comments>
		
		<dc:creator><![CDATA[Jamie]]></dc:creator>
		<pubDate>Thu, 23 May 2013 12:19:00 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[TCL]]></category>
		<category><![CDATA[Dev Hour]]></category>
		<category><![CDATA[Sockets]]></category>
		<category><![CDATA[Web Development]]></category>
		<guid isPermaLink="false">https://www.devhour.net/?p=22</guid>

					<description><![CDATA[<p>So if you frequent IRC often like I do, you may notice some eggdrop bots displaying messages which come from a website. The most common times you will see this sort of script in action is when they are hooked into forum posts or torrent listings. You will more than likely have seen messages saying [&#8230;]</p>
<p>The post <a href="https://www.devhour.net/sending-messages-to-an-eggdrop-via-php/">Sending messages to an Eggdrop via PHP</a> first appeared on <a href="https://www.devhour.net">Devhour</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>So if you frequent IRC often like I do, you may notice some eggdrop bots displaying messages which come from a website. The most common times you will see this sort of script in action is when they are hooked into forum posts or torrent listings. You will more than likely have seen messages saying “New forum topic…X Y by Z” or “[PRE] such.and.such.movie.torrent”.&nbsp;Today I’m going to show you how to do that by sending messages to an eggdrop via PHP.</p>



<p>It’s actually relatively easy (as you are about to see) and really only requires a few lines of PHP code on the web end and the same again in TCL on the eggdrop. The general idea is that the eggdrop will open up a socket and sit listening waiting for data to come through before displaying the message. For the PHP end it is as simple as connecting to said socket and sending through some data.</p>



<p>First things first, the TCL script sock-listen.tcl:</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#575279;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#faf4ed"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="set output_chan &quot;#chan&quot;
# eggdrop port to bindset 
port 7878
# eggdrop ip to bind
set host xxx.xxx.xxx.xxx
set serverSocket [socket -server main -myaddr $host $port]
proc main { sock host port } {
  fconfigure $sock -buffering line
  fileevent $sock readable [action $sock $host $port]
}
proc action { chan host port } {
  global output_chan
  if {![eof $chan]} {
    set soc_data [gets $chan]
    if {$soc_data != &quot;&quot;} {
      putquick &quot;PRIVMSG $output_chan :$host | $port $soc_data&quot;
    }
  } { close $chan }
}
putlog &quot;sock-listen.tcl loaded&quot;" style="color:#575279;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #286983">set</span><span style="color: #575279"> output_chan </span><span style="color: #EA9D34">&quot;#chan&quot;</span></span>
<span class="line"><span style="color: #797593; font-style: italic">#</span><span style="color: #9893A5; font-style: italic"> eggdrop port to bindset </span></span>
<span class="line"><span style="color: #575279">port </span><span style="color: #D7827E">7878</span></span>
<span class="line"><span style="color: #797593; font-style: italic">#</span><span style="color: #9893A5; font-style: italic"> eggdrop ip to bind</span></span>
<span class="line"><span style="color: #286983">set</span><span style="color: #575279"> host xxx.xxx.xxx.xxx</span></span>
<span class="line"><span style="color: #286983">set</span><span style="color: #575279"> serverSocket [</span><span style="color: #286983">socket</span><span style="color: #575279"> -server main -myaddr </span><span style="color: #797593; font-style: italic">$</span><span style="color: #B4637A; font-style: italic">host</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #B4637A; font-style: italic">port</span><span style="color: #575279">]</span></span>
<span class="line"><span style="color: #286983">proc</span><span style="color: #575279"> </span><span style="color: #D7827E">main</span><span style="color: #575279"> { sock host port } {</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #286983">fconfigure</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #B4637A; font-style: italic">sock</span><span style="color: #575279"> -buffering line</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #286983">fileevent</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #B4637A; font-style: italic">sock</span><span style="color: #575279"> readable [action </span><span style="color: #797593; font-style: italic">$</span><span style="color: #B4637A; font-style: italic">sock</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #B4637A; font-style: italic">host</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #B4637A; font-style: italic">port</span><span style="color: #575279">]</span></span>
<span class="line"><span style="color: #575279">}</span></span>
<span class="line"><span style="color: #286983">proc</span><span style="color: #575279"> </span><span style="color: #D7827E">action</span><span style="color: #575279"> { chan host port } {</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #286983">global</span><span style="color: #575279"> output_chan</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #286983">if</span><span style="color: #575279"> {![</span><span style="color: #286983">eof</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #B4637A; font-style: italic">chan</span><span style="color: #575279">]} {</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #286983">set</span><span style="color: #575279"> soc_data [</span><span style="color: #286983">gets</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #B4637A; font-style: italic">chan</span><span style="color: #575279">]</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #286983">if</span><span style="color: #575279"> {</span><span style="color: #797593; font-style: italic">$</span><span style="color: #B4637A; font-style: italic">soc_data</span><span style="color: #575279"> != </span><span style="color: #EA9D34">&quot;&quot;</span><span style="color: #575279">} {</span></span>
<span class="line"><span style="color: #575279">      putquick </span><span style="color: #EA9D34">&quot;PRIVMSG </span><span style="color: #797593; font-style: italic">$</span><span style="color: #B4637A; font-style: italic">output_chan</span><span style="color: #EA9D34"> :</span><span style="color: #797593; font-style: italic">$</span><span style="color: #B4637A; font-style: italic">host</span><span style="color: #EA9D34"> | </span><span style="color: #797593; font-style: italic">$</span><span style="color: #B4637A; font-style: italic">port</span><span style="color: #EA9D34"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #B4637A; font-style: italic">soc_data</span><span style="color: #EA9D34">&quot;</span></span>
<span class="line"><span style="color: #575279">    }</span></span>
<span class="line"><span style="color: #575279">  } { </span><span style="color: #286983">close</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #B4637A; font-style: italic">chan</span><span style="color: #575279"> }</span></span>
<span class="line"><span style="color: #575279">}</span></span>
<span class="line"><span style="color: #575279">putlog </span><span style="color: #EA9D34">&quot;sock-listen.tcl loaded&quot;</span></span></code></pre><span style="display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#faf4ed;color:#625c88;font-size:12px;line-height:1;position:relative">TCL</span></div>



<p>That’s it! Simple enough. Create a socket which will sit and listen waiting for something to connect and send through data. Once a connection is made call the ‘action’ function which will &nbsp;send a message to the channel with the host, port and socket data.</p>



<p>To get that up and running just copy sock-listen.tcl into the scripts folder of your eggdrop and include it in your config file. Don’t forget to change the channel, IP address and port. Once you rehash or reconnect the bot the socket will automatically start listening.</p>



<p>Now to send messages from PHP is very straight forward, just open up a socket and connect to the IP/port specified in the TCL script. Send through a message and watch it come out the other end (IRC channel).</p>



<p>To do this we create a php file called send.php and place the following code:</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#575279;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#faf4ed"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="&lt;?php
//eggdrop port
$egg_port = 7878;
//eggdrop ip
$egg_ip = 'xxx.xxx.xxx.xxx';
// message to send
$msg = 'Sent from a PHP page';
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($sock, $egg_ip, $egg_port) or die ('failed to connect!');
socket_write($sock, $msg);
socket_close($sock);
?&gt;" style="color:#575279;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #286983">&lt;?php</span></span>
<span class="line"><span style="color: #797593; font-style: italic">//</span><span style="color: #9893A5; font-style: italic">eggdrop port</span></span>
<span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">egg_port </span><span style="color: #286983">= 7878</span><span style="color: #797593">;</span></span>
<span class="line"><span style="color: #797593; font-style: italic">//</span><span style="color: #9893A5; font-style: italic">eggdrop ip</span></span>
<span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">egg_ip </span><span style="color: #286983">= </span><span style="color: #EA9D34">&#39;xxx.xxx.xxx.xxx&#39;</span><span style="color: #797593">;</span></span>
<span class="line"><span style="color: #797593; font-style: italic">//</span><span style="color: #9893A5; font-style: italic"> message to send</span></span>
<span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">msg </span><span style="color: #286983">= </span><span style="color: #EA9D34">&#39;Sent from a PHP page&#39;</span><span style="color: #797593">;</span></span>
<span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">sock </span><span style="color: #286983">=</span><span style="color: #D7827E"> socket_create</span><span style="color: #797593">(</span><span style="color: #286983">AF_INET</span><span style="color: #797593">,</span><span style="color: #286983"> SOCK_STREAM</span><span style="color: #797593">,</span><span style="color: #286983"> SOL_TCP</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #B4637A; font-style: italic">socket_connect</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">sock</span><span style="color: #797593">,</span><span style="color: #286983"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">egg_ip</span><span style="color: #797593">,</span><span style="color: #286983"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">egg_port</span><span style="color: #797593">)</span><span style="color: #D7827E"> or die </span><span style="color: #797593">(</span><span style="color: #EA9D34">&#39;failed to connect!&#39;</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #B4637A; font-style: italic">socket_write</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">sock</span><span style="color: #797593">,</span><span style="color: #286983"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">msg</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #B4637A; font-style: italic">socket_close</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">sock</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #286983">?&gt;</span></span></code></pre><span style="display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#faf4ed;color:#625c88;font-size:12px;line-height:1;position:relative">PHP</span></div>



<p>Again, easy enough. Make sure you set the correct IP address and port and then upload the file. Point your browser to it and watch the magic happen. You should see the following message appear on the IRC channel:</p>



<p><strong>xxx.xxx.xxx.xxx | 7878 | Sent from a PHP page</strong></p>



<p>To make it easier I’ve added a small form which will allow you to customize the message to your liking. Again send.php:</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#575279;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#faf4ed"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="&lt;?php
//eggdrop port$egg_port = 7878;
//eggdrop ip
$egg_ip = 'xxx.xxx.xxx.xxx';

if (isset($_POST['message'])) {
  $msg = $_POST['message'];   
  $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  socket_connect($sock, $egg_ip, $egg_port) or die ('failed to connect!');
  socket_write($sock, $msg);
  socket_close($sock);
  header(&quot;Location: send.php&quot;);
}
?&gt;
&lt;form action='send.php' method='POST'&gt;
&lt;input type='text' name='message' cols='20'&gt;&lt;br&gt;
&lt;input type='submit' value='send'&gt;
&lt;/form&gt;" style="color:#575279;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #286983">&lt;?php</span></span>
<span class="line"><span style="color: #797593; font-style: italic">//</span><span style="color: #9893A5; font-style: italic">eggdrop port$egg_port = 7878;</span></span>
<span class="line"><span style="color: #797593; font-style: italic">//</span><span style="color: #9893A5; font-style: italic">eggdrop ip</span></span>
<span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">egg_ip </span><span style="color: #286983">= </span><span style="color: #EA9D34">&#39;xxx.xxx.xxx.xxx&#39;</span><span style="color: #797593">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #286983">if </span><span style="color: #797593">(</span><span style="color: #B4637A; font-style: italic">isset</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">_POST</span><span style="color: #797593">[</span><span style="color: #EA9D34">&#39;message&#39;</span><span style="color: #797593">]))</span><span style="color: #286983"> </span><span style="color: #797593">{</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">msg </span><span style="color: #286983">= </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">_POST</span><span style="color: #797593">[</span><span style="color: #EA9D34">&#39;message&#39;</span><span style="color: #797593">];</span><span style="color: #286983">   </span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">sock </span><span style="color: #286983">=</span><span style="color: #D7827E"> socket_create</span><span style="color: #797593">(</span><span style="color: #286983">AF_INET</span><span style="color: #797593">,</span><span style="color: #286983"> SOCK_STREAM</span><span style="color: #797593">,</span><span style="color: #286983"> SOL_TCP</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #B4637A; font-style: italic">socket_connect</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">sock</span><span style="color: #797593">,</span><span style="color: #286983"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">egg_ip</span><span style="color: #797593">,</span><span style="color: #286983"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">egg_port</span><span style="color: #797593">)</span><span style="color: #D7827E"> or die </span><span style="color: #797593">(</span><span style="color: #EA9D34">&#39;failed to connect!&#39;</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #B4637A; font-style: italic">socket_write</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">sock</span><span style="color: #797593">,</span><span style="color: #286983"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">msg</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #B4637A; font-style: italic">socket_close</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">sock</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #B4637A; font-style: italic">header</span><span style="color: #797593">(</span><span style="color: #EA9D34">&quot;Location: send.php&quot;</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #797593">}</span></span>
<span class="line"><span style="color: #286983">?&gt;</span></span>
<span class="line"><span style="color: #286983">&lt;form action=</span><span style="color: #EA9D34">&#39;send.php&#39;</span><span style="color: #286983"> method=</span><span style="color: #EA9D34">&#39;POST&#39;</span><span style="color: #286983">&gt;</span></span>
<span class="line"><span style="color: #286983">&lt;input type=</span><span style="color: #EA9D34">&#39;text&#39;</span><span style="color: #286983"> name=</span><span style="color: #EA9D34">&#39;message&#39;</span><span style="color: #286983"> cols=</span><span style="color: #EA9D34">&#39;20&#39;</span><span style="color: #286983">&gt;&lt;</span><span style="color: #D7827E">br</span><span style="color: #286983">&gt;</span></span>
<span class="line"><span style="color: #286983">&lt;input type=</span><span style="color: #EA9D34">&#39;submit&#39;</span><span style="color: #286983"> value=</span><span style="color: #EA9D34">&#39;send&#39;</span><span style="color: #286983">&gt;</span></span>
<span class="line"><span style="color: #286983">&lt;/form&gt;</span></span></code></pre><span style="display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#faf4ed;color:#625c88;font-size:12px;line-height:1;position:relative">PHP</span></div>



<p>It isn’t all that secure but gives you a general idea of what you can do.&nbsp;To implement it into the examples I posted at the start all you really need to do is edit the submit/post PHP files and place the socket code above with a customized message in.</p>



<p>This isn’t limited to just PHP. Something similar could easily be set up in a number or any other languages as well. Simply by opening the socket and sending the data.</p>



<p>Something simple for &nbsp;the day. Have a play around and let me know if you make any improvements. Feel free to ask any questions, I’ll do my best to answer them&nbsp;<img decoding="async" src="https://web.archive.org/web/20150313032438im_/https://www.devhour.net/wp-includes/images/smilies/icon_smile.gif" alt=":)"></p>



<p>Also feel free to follow me on twitter: <a href="https://twitter.com/JAGracie">@JAGracie</a></p><p>The post <a href="https://www.devhour.net/sending-messages-to-an-eggdrop-via-php/">Sending messages to an Eggdrop via PHP</a> first appeared on <a href="https://www.devhour.net">Devhour</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://www.devhour.net/sending-messages-to-an-eggdrop-via-php/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Scraping data with PHP and cURL</title>
		<link>https://www.devhour.net/scraping-data-with-php-and-curl/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=scraping-data-with-php-and-curl</link>
					<comments>https://www.devhour.net/scraping-data-with-php-and-curl/#respond</comments>
		
		<dc:creator><![CDATA[Jamie]]></dc:creator>
		<pubDate>Sun, 12 May 2013 12:17:00 +0000</pubDate>
				<category><![CDATA[cURL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Dev Hour]]></category>
		<category><![CDATA[Web Development]]></category>
		<guid isPermaLink="false">https://www.devhour.net/?p=20</guid>

					<description><![CDATA[<p>I’m working on another idea which I hope to release soon which involves scraping websites using PHP and cURL. I don’t want to give too much away before I release the website so I won’t go into too much detail. However, what I can tell you is that it required me to go out and [&#8230;]</p>
<p>The post <a href="https://www.devhour.net/scraping-data-with-php-and-curl/">Scraping data with PHP and cURL</a> first appeared on <a href="https://www.devhour.net">Devhour</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>I’m working on another idea which I hope to release soon which involves scraping websites using PHP and cURL.</p>



<p>I don’t want to give too much away before I release the website so I won’t go into too much detail. However, what I can tell you is that it required me to go out and &nbsp;get a lot of data from external websites using variables passed through from a form on my end.</p>



<p>I originally started out using a piece of python software called <a href="https://scrapy.org/">Scrapy</a> which worked very well, but the logistics of using that and either storing the data or displaying it on a webpage became too much of a hassle so I instead opted to go for PHP and cURL.</p>



<p>For the PHP side I’m using the framework <a href="https://www.codeigniter.com/">Codeigniter</a> which is a very easy and very speedy framework which is perfect for what I wanted to do.</p>



<p>The basic flow of how everything works is:</p>



<ol>
<li>The form is filled out and submitted</li>



<li>Data from the form is sent to the external website in a cURL request</li>



<li>The webpage content is then returned</li>



<li>From there the data can be formatted and displayed accordingly</li>
</ol>



<p>To do this with PHP and cURL is a fairly straight forward process and I’ll show you how to go about it. The only real issue you may come across is that when forms come into play you need to make sure each and every form element is included in the call.</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#575279;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#faf4ed"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="$url = 'http://www.website.com/login.php';
$postdata = array('username' =&gt; &quot;Jamie&quot;,'password' =&gt; &quot;password&quot;);
$ch = curl_init();
if($ch){
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
  curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt'); // set cookie file to given file   
  curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt'); // set same file as cookie jar   
  $content = curl_exec($ch);
  $headers = curl_getinfo($ch);
  curl_close($ch);
  // Debug option
  // print_r($headers);
  if($headers['http_code'] == 200){
    echo $content;
  }
}" style="color:#575279;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">url</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #EA9D34">&#39;http://www.website.com/login.php&#39;</span><span style="color: #797593">;</span></span>
<span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">postdata</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #B4637A; font-style: italic">array</span><span style="color: #797593">(</span><span style="color: #EA9D34">&#39;username&#39;</span><span style="color: #575279"> </span><span style="color: #286983">=&gt;</span><span style="color: #575279"> </span><span style="color: #EA9D34">&quot;Jamie&quot;</span><span style="color: #797593">,</span><span style="color: #EA9D34">&#39;password&#39;</span><span style="color: #575279"> </span><span style="color: #286983">=&gt;</span><span style="color: #575279"> </span><span style="color: #EA9D34">&quot;password&quot;</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">ch</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #B4637A; font-style: italic">curl_init</span><span style="color: #797593">();</span></span>
<span class="line"><span style="color: #286983">if</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">ch</span><span style="color: #797593">){</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #B4637A; font-style: italic">curl_setopt</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">ch</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #286983">CURLOPT_URL</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">url</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #B4637A; font-style: italic">curl_setopt</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">ch</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #286983">CURLOPT_CONNECTTIMEOUT</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #D7827E">15</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #B4637A; font-style: italic">curl_setopt</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">ch</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #286983">CURLOPT_RETURNTRANSFER</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #D7827E">true</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #B4637A; font-style: italic">curl_setopt</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">ch</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #286983">CURLOPT_POST</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #D7827E">1</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #B4637A; font-style: italic">curl_setopt</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">ch</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #286983">CURLOPT_POSTFIELDS</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">postdata</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #B4637A; font-style: italic">curl_setopt</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">ch</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #286983">CURLOPT_COOKIEFILE</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #EA9D34">&#39;cookies.txt&#39;</span><span style="color: #797593">);</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">//</span><span style="color: #9893A5; font-style: italic"> set cookie file to given file   </span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #B4637A; font-style: italic">curl_setopt</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">ch</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #286983">CURLOPT_COOKIEJAR</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #EA9D34">&#39;cookies.txt&#39;</span><span style="color: #797593">);</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">//</span><span style="color: #9893A5; font-style: italic"> set same file as cookie jar   </span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">content</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #B4637A; font-style: italic">curl_exec</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">ch</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">headers</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #B4637A; font-style: italic">curl_getinfo</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">ch</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #B4637A; font-style: italic">curl_close</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">ch</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #797593">  </span><span style="color: #797593; font-style: italic">//</span><span style="color: #9893A5; font-style: italic"> Debug option</span></span>
<span class="line"><span style="color: #797593">  </span><span style="color: #797593; font-style: italic">//</span><span style="color: #9893A5; font-style: italic"> print_r($headers);</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #286983">if</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">headers</span><span style="color: #797593">[</span><span style="color: #EA9D34">&#39;http_code&#39;</span><span style="color: #797593">]</span><span style="color: #575279"> </span><span style="color: #286983">==</span><span style="color: #575279"> </span><span style="color: #D7827E">200</span><span style="color: #797593">){</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #B4637A; font-style: italic">echo</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">content</span><span style="color: #797593">;</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #797593">}</span></span>
<span class="line"><span style="color: #797593">}</span></span></code></pre><span style="display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#faf4ed;color:#625c88;font-size:12px;line-height:1;position:relative">PHP</span></div>



<p>That’s the entire call and will return the html contents of website.com/login.php. I’ll go through the above code piece by piece and give a run down on each of the different parts.</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#575279;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#faf4ed"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="$url = 'http://www.website.com/login.php';
$postdata = array('username' =&gt; &quot;Jamie&quot;, 'password' =&gt; &quot;password&quot;);" style="color:#575279;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">url</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #EA9D34">&#39;http://www.website.com/login.php&#39;</span><span style="color: #797593">;</span></span>
<span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">postdata</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #B4637A; font-style: italic">array</span><span style="color: #797593">(</span><span style="color: #EA9D34">&#39;username&#39;</span><span style="color: #575279"> </span><span style="color: #286983">=&gt;</span><span style="color: #575279"> </span><span style="color: #EA9D34">&quot;Jamie&quot;</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #EA9D34">&#39;password&#39;</span><span style="color: #575279"> </span><span style="color: #286983">=&gt;</span><span style="color: #575279"> </span><span style="color: #EA9D34">&quot;password&quot;</span><span style="color: #797593">);</span></span></code></pre><span style="display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#faf4ed;color:#625c88;font-size:12px;line-height:1;position:relative">PHP</span></div>



<p>Firstly, the url variable should be self explanatory and the postdata is just a simple array which contains the form elements that are required to login with (in this case a username and password).</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#575279;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#faf4ed"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="$ch = curl_init();if($ch){" style="color:#575279;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">ch</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #B4637A; font-style: italic">curl_init</span><span style="color: #797593">();</span><span style="color: #286983">if</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">ch</span><span style="color: #797593">){</span></span></code></pre><span style="display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#faf4ed;color:#625c88;font-size:12px;line-height:1;position:relative">PHP</span></div>



<p>Create a new curl object and if all is well continue on.</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#575279;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#faf4ed"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt'); // set cookie file to given file
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt'); // set same file as cookie jar" style="color:#575279;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #B4637A; font-style: italic">curl_setopt</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">ch</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #286983">CURLOPT_URL</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">url</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #B4637A; font-style: italic">curl_setopt</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">ch</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #286983">CURLOPT_CONNECTTIMEOUT</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #D7827E">15</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #B4637A; font-style: italic">curl_setopt</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">ch</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #286983">CURLOPT_RETURNTRANSFER</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #D7827E">true</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #B4637A; font-style: italic">curl_setopt</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">ch</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #286983">CURLOPT_POST</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #D7827E">1</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #B4637A; font-style: italic">curl_setopt</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">ch</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #286983">CURLOPT_POSTFIELDS</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">postdata</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #B4637A; font-style: italic">curl_setopt</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">ch</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #286983">CURLOPT_COOKIEFILE</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #EA9D34">&#39;cookies.txt&#39;</span><span style="color: #797593">);</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">//</span><span style="color: #9893A5; font-style: italic"> set cookie file to given file</span></span>
<span class="line"><span style="color: #B4637A; font-style: italic">curl_setopt</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">ch</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #286983">CURLOPT_COOKIEJAR</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #EA9D34">&#39;cookies.txt&#39;</span><span style="color: #797593">);</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">//</span><span style="color: #9893A5; font-style: italic"> set same file as cookie jar</span></span></code></pre><span style="display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#faf4ed;color:#625c88;font-size:12px;line-height:1;position:relative">PHP</span></div>



<p>These are all curl options which I am going to use. You can view the rest of the different options over at the <a href="https://php.net/manual/en/function.curl-setopt.php">php website</a>. The ones we are using and basically all to do with logging in. Storing the cookies and passing through the post data are the main ones to take note of.</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#575279;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#faf4ed"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="$content = curl_exec($ch);
$headers = curl_getinfo($ch);
curl_close($ch);" style="color:#575279;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">content</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #B4637A; font-style: italic">curl_exec</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">ch</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">headers</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #B4637A; font-style: italic">curl_getinfo</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">ch</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #B4637A; font-style: italic">curl_close</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">ch</span><span style="color: #797593">);</span></span></code></pre><span style="display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#faf4ed;color:#625c88;font-size:12px;line-height:1;position:relative">PHP</span></div>



<p>Last but not least, execute the curl request passing through the options we used, set the returned content to a variable and also grab the headers before finally closing the curl object.</p>



<p>As you will note in the original code I use the headers variable as a debug option. This is very handy, in particular the header_code which can be very useful. If you ever find that something isn’t working, double check that you are getting a 200 code and not a 400/501.</p>



<p>From there you can grab/scrape the content and data to your hearts content. A great thing is now that you have received and stored the cookies from logging in, you have access to ‘authenticated only’ sections of the website. So you can go away and run more curl requests to get those areas of the website.</p>



<p>I was about to end it there but one other important piece I have come across is that some forms that you fill out will actually re-direct you to different parts of the website after submit. It’s fairly easy to identify because you will get a header code of 302 and the great thing is that you also get a redirect_url in the headers. All you need to do is make another curl request using the redirect url you received.</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#575279;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#faf4ed"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="// original curl request up here
if ($headers['http_code'] == 302){
  $ch = @curl_init();
  curl_setopt($ch, CURLOPT_URL, $headers['redirect_url']);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt'); // set cookie file to given file   
  curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt'); // set same file as cookie jar   
  $content = curl_exec($ch);
}" style="color:#575279;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #797593; font-style: italic">//</span><span style="color: #9893A5; font-style: italic"> original curl request up here</span></span>
<span class="line"><span style="color: #286983">if</span><span style="color: #575279"> </span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">headers</span><span style="color: #797593">[</span><span style="color: #EA9D34">&#39;http_code&#39;</span><span style="color: #797593">]</span><span style="color: #575279"> </span><span style="color: #286983">==</span><span style="color: #575279"> </span><span style="color: #D7827E">302</span><span style="color: #797593">){</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">ch</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #286983">@</span><span style="color: #B4637A; font-style: italic">curl_init</span><span style="color: #797593">();</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #B4637A; font-style: italic">curl_setopt</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">ch</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #286983">CURLOPT_URL</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">headers</span><span style="color: #797593">[</span><span style="color: #EA9D34">&#39;redirect_url&#39;</span><span style="color: #797593">]);</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #B4637A; font-style: italic">curl_setopt</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">ch</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #286983">CURLOPT_CONNECTTIMEOUT</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #D7827E">15</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #B4637A; font-style: italic">curl_setopt</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">ch</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #286983">CURLOPT_RETURNTRANSFER</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #D7827E">true</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #B4637A; font-style: italic">curl_setopt</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">ch</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #286983">CURLOPT_COOKIEFILE</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #EA9D34">&#39;cookies.txt&#39;</span><span style="color: #797593">);</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">//</span><span style="color: #9893A5; font-style: italic"> set cookie file to given file   </span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #B4637A; font-style: italic">curl_setopt</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">ch</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #286983">CURLOPT_COOKIEJAR</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #EA9D34">&#39;cookies.txt&#39;</span><span style="color: #797593">);</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">//</span><span style="color: #9893A5; font-style: italic"> set same file as cookie jar   </span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">content</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #B4637A; font-style: italic">curl_exec</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">ch</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #797593">}</span></span></code></pre><span style="display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#faf4ed;color:#625c88;font-size:12px;line-height:1;position:relative">PHP</span></div>



<p>Once you have gotten the content you require you then need to get access to the specific data or text you’re after. I’m going to show you how to do this using xpath in another post, so keep your eyes out for that.</p>



<p>Like always, if you have any comments or questions feel free to post and I’ll do my best to answer ’em.</p>



<p>Follow me on twitter <a href="https://twitter.com/JAGracie">@JAGracie</a></p><p>The post <a href="https://www.devhour.net/scraping-data-with-php-and-curl/">Scraping data with PHP and cURL</a> first appeared on <a href="https://www.devhour.net">Devhour</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://www.devhour.net/scraping-data-with-php-and-curl/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
