The SEO PHP Crash Course Tutorial
|
| |
![]() | |
Most of you have probably picked up on the fact I have no problem with helping newbies out. So I’ve been talking to a few people lately, several of whom do not know any PHP. So I’m going to do a super basic PHP tutorial, written for those who want to use PHP for SEO purposes. So we’ll cover basic data handling, web page retrieval, etc.
This is the ultra basics, and PHP can, and should, do much more. I reccomend you find some other tutorials as well, and head over to php.net (their function searh is awesome) if you’re serious about learning to code.
If you have nowhere to test this(web server, et) I reccomend you search for PHPerl Move on Google, and give that a download. It will open a server on port 180 for you.
Note: If you copy and paste this code, notepad might not like the quotes, so do a search and replace for them, and replace it with the normal quote.
The Super Mega Basics
- Variables
- In PHP, variables are always referenced with a $ sign. So $myVariable would create one, and would forever be referenced as such.
- Variables are case sensitive, and do not need to be declared prior to use. So if your variable doesn’t seem to have any data, and should, check the capitalization.
- Numbers that you declare yourself do not have quotes around them. So $myVariable=3; is perfectly acceptable.
- If you read in a number from a text file or a webpage, PHP still thinks it’s a string. To fix this, say $myNum=intval($originalVariable);
- Strings meanwhile, have a set of quotes around them. Always. $myVariable=”My Variable Biotch”; is how it’s supposed to be.
- Looping
- Loops are what you use when you want a certain action to happen until a certain condition is met.
- There are 2 kinds of loops that you really need to know. The while loop, and the for loop.
- The While Loop
- Format:
- while(condition)
{
//action
}
- while(condition)
- Example
- $x=0; //declare our variable
while($x<20) //the loop
{
$x=$x+5; //this action will be performed until $x is greater than 20.
}
- $x=0; //declare our variable
- Format:
- The For Loop (used when you need to count the number of times something is done)
- Format:
for($variable=startPosition; $condition; $variable++)
{
//do this action
} - Example:
for($i=0; $i<10; $i++)
{
echo $i.”<br>”;//echo outputs text to the browser. This prints the value of “i”, then an HTML line break.
}
- Format:
- The While Loop
- Conditional Statements - A way of saying “if this is true, then do this, otherwise, do this.”
- Format:
if($condition1)
{
//do this
}
else if($condition2)//this is only checked of condition1 was not true. Else if is optional.
{
//do this
}
else //if none of the above were true, then do this. Else is optional.
{
//do this
}
- Format:
- Combining Variables - How to combine multiple strings, or strings and numbers, or numbers and numbers. For this one, I’ll just use examples. PHP uses a period to seperate them.
- $str=”I am a string. “;
$str2=”I am also a string”;
echo $str.$str2; //outputs: I am a string. I am also a string
echo “<br>”;//outputs a line break, to drop a line in the browser
echo $str.” I am also a string<br>”;//outputs the same as the above two lines. - $str=”apple “;
$num=2;
echo $str.” “.$num; //outputs: apple 2 - $num1=8;
$num2=3;
echo $num1+$num2; //outputs: 11
- $str=”I am a string. “;
- Arrays - These are VERY needed. Pay attention. It’s a way of storing a lot of information, in one variable. If you ever need to see what’s in an array, type print_r($array); where $array is your array name. It will output everything.
- $myArray=array();
$myArray[0]=”Woot”;
$myArray[1]=”What?”;
$myArray[2]=”I was wooting”;- echo $myArray[0].”<br>”; //outputs:woot, and a line break.
echo $myArray[1].”<br>”;//outputs: what?, and a line break. - //this version combines it with a loop
for($i=0; $i<sizeof($myArray); $i++)//sizeof() returns the number of values in the array
{
echo $myArray[$i].”<br>”;//outputs whatever is in that location, and a line break.
}
- echo $myArray[0].”<br>”; //outputs:woot, and a line break.
- $myArray=array();
Now For the Juicy SEO Stuff.
- Getting an HTML Page, or really any page Over the Internet.
- The file_get_contents method. Note: Some webhosts do not allow this for web pages.
$url=http://www.slightlyshadyseo.com;
echo file_get_contents($url);//outputs the contents of slightly shady SEO - The CURL Method
- Requires you to enable CURL in your php.ini file
- Info on this is available in a previous entry (CURL Tutorial). It’s long, so I don’t want to repost it.
- This is sneakier, as you can forge user agents, handle redirects, and send post data. Oh yeah, and use proxies.
- The file_get_contents method. Note: Some webhosts do not allow this for web pages.
- Extracting Data
- There are many ways to do this. I’m going to use explode, because it’s the fastest to code, and doesn’t require many functions to use. Explode splits a string into an array. You give it a string, and that is where it splits.
- Basic Example:
$myString=”I am not shady”;
$spl=explode(”not”,$myString);
echo $spl[0].” “.$spl[1]; //outputs: I am shady - function extractData($startString, $endString, $dataToSplit) //this is how you create your own function.
{
$spl=explode($startString, $dataToSplit);
$spl2=explode($endString,$spl[1]);
return($spl2[0]);
}
//now we use the function
$myString=”Junkity Junkity Junk <post>This is my post Data </post> More Junk I don’t want”;
echo extractData(”<post>”,”</post>”,$myString); //outputs: This is my post Data
- Basic Example:
- There are many ways to do this. I’m going to use explode, because it’s the fastest to code, and doesn’t require many functions to use. Explode splits a string into an array. You give it a string, and that is where it splits.
- Reading Files
- $data=file_get_contents(”./myFile.txt”);
- Writing Files
- $handle=fopen(”./output.txt”,”a+”);//a+ might not be the right flag for you. Look up fopen on php.net to get more flags. They determine if the file will be created if it doesn’t exist, and whether you should start writing data at the beginning or end of the file.
fwrite($handle, $myData);//writes myData to the file
fclose($handle);//closes the file for reading/writing
- $handle=fopen(”./output.txt”,”a+”);//a+ might not be the right flag for you. Look up fopen on php.net to get more flags. They determine if the file will be created if it doesn’t exist, and whether you should start writing data at the beginning or end of the file.
Alright Kids, Time to Bring it All Together, and Extract Some Data from an RSS Feed. From Google Blog Search. That’s Right. We’re making a RSS reader.
Sorry about the dumbass formatting, it’s the best I could find. This fixes the quote problem described above, but if you want to copy and paste, double click the upper left hand corner to kill the line numbers.
$url=”http://blogsearch.google.com/blogsearch_feeds?hl=en&q=seo&ie=utf-8&num=10&output=rss”;//search google blog search for “seo”
$data=file_get_contents($url);//get the url. Replace this with curl if you can’t use file_get_contents
$spl=explode(”
for($i=1; $i
$spl2=explode(”", $spl[$i]);//get the current item. Everything inbetween
$currentData=$spl2[0];//this contains all our data, since was at the BACK of our data.
$title=extractData(”", $currentData);
$link=extractData(”
$description=extractData(”
$title=strip_tags($title);//having converted everything back to HTML, we’re stripping all tags.
//now we’re going to do to the other fields (link, description) what we did to the title
$link=html_entity_decode($link);
$link=strip_tags($link);
$description=html_entity_decode($description);
$description=strip_tags($description);
//now we’re going to output the data
echo ““.$title.” “;//output our title
echo “(Source)
“; //output our link
echo $description.”
“; //output the description
echo ”
“; //output a horizontal rule to make it pretty}
function extractData($startString, $endString, $dataToSplit) //this is how you create your own function.
{
$spl=explode($startString, $dataToSplit);
$spl2=explode($endString,$spl[1]);
return($spl2[0]);
}?>





















November 24th, 2007 at 4:37 am
Really love your stuff
What bookmark plug in do you use it is really nice
December 11th, 2007 at 7:38 am
Amazing,I have to say simply the best !
December 20th, 2007 at 10:50 am
I wonder whats the end of ” for($i=1; $i { ” ? and how it works ?
December 20th, 2007 at 11:17 am
[code]
Wow. Wordpress completeley massacared that post. I guess some plugin didnt upgrade right.
for($i=1; $i < sizeof($spl); $i++)
{
}
[/code]
December 20th, 2007 at 9:59 pm
XMCP ,how to contact you? whats your email? Would you send me a sricpt by email ?
December 31st, 2007 at 1:22 am
I’ve been trying to get the file_get_contents thing to work for me but I can’t seem to get it working. I keep get a “parse error” telling me it doesn’t understand the “:” character on the line where I have url=domainehere.com;
Any ideas? I’m with dreamhost, so IDK if they allow the use of that function, as you mentioned…
December 31st, 2007 at 2:54 am
Hey Gab. Read Dreamhosts rules. No file_get_contents except for internal files. Might wanna read up on CURL.