Category Archives: Programming

Server Sent Events using Laravel and Vue

Recently I was working on a new project, where I had to stream stock data and display it on a chart. My first instinct was to setup a loop using setTimeout, but I quickly realized that wouldn’t work for my exact needs. Stock prices change so frequently, often happening several times a second. Doing an ajax call every second seemed like the worst overhead baggage I could possibly program myself into. After doing a bit of research, I stumbled upon Server-Sent Events.  Continue Reading

Use PHP to Retrieve All Links on a Page

I was developing a project recently, where I had the need to get all the links on a certain page. I first turned to a JavaScript library, like Jasmine.

I really wanted to do this within my PHP code, so I ended up going with this solution:


$html = file_get_contents('http://www.example.com');

$dom = new DOMDocument();
@$dom->loadHTML($html);

// grab all the on the page
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");

for ($i = 0; $i < $hrefs->length; $i++) {
    $href = $hrefs->item($i);
    $url = $href->getAttribute('href');
    echo $url;
}

Test Driven API Development

DotDev has posted a great tutorial on Test Driven API Development. Even though I already develop test driven APIs, I still love reading articles like this to see how others are solving problems.

I have rolled my own Transformers in the past, and I am very excited about the Fractal package they reference in this article. I’m also excited about the API documentation generator, and I think I’m going to give that a try this weekend.

Direct Link

Show Number of Facebook Likes in PHP

If you run a Facebook page, and you are starting to build up some good numbers, you may want to show them off on your website. Facebook allows you to embed a Facebook widget on your homepage, but that means you are using third-party scripts, and you can’t control the design. Here is a simple PHP snippet for displaying your Facebook Likes on your website.


$facebook_page_id = "127012630783508";
$results = @simplexml_load_file("http://api.facebook.com/restserver.php?method=facebook.fql.query&query=SELECT%20fan_count%20FROM%20page%20WHERE%20page_id=${facebook_page_id}");
echo $results->page->fan_count;