you forgot to open an output document. When using PDFlib, you always create a new output PDF, and so using $p->begin_document() is necessary.
if ($p->begin_document($outfile, "") == 0)
throw new Exception("Error: " . $p->get_errmsg());
Then you have to open a page, place your page from the input document and then you can add the rectangle.
You find a very similar sample in the PDFlib cookbook: http://www.pdflib.com/pdflib-cookbook/pdf-import/stamp-pages/php-stamp-pages/ where a text stamp is placed on the page. In your case, you should place the rect.
I guess, it's worth to point to the PDFlib Tutorial, chapter 7.3 "Importing PDF Pages with PDI".