Thursday, September 30, 2010

Include a PHP file in symfony

Sometimes you might want to include a file from an external library in symfony.

Copy the according library (third_party_lib) in symfony_project_folder/lib. To include a file from this folder in another php file do:

include_once sfConfig::get('sf_lib_dir')."/third_party_lib/File.php";

PHP get absolute path of file

To get the absolute path of a php file add:

$path = getcwd();
echo $path;

Be aware:

This will give you the absolute path of the file in the file structure, not the URL!

Exception CURL error: SSL certificate problem, verify that the CA cert is OK

When using curl and sending an HTTPS request you could run into an error.

If you are getting "Exception CURL error: SSL certificate problem, verify that the CA cert is OK" or similar, that indicates problem with the remote SSL certificate.

To fix this you can add:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

to the curl request.

On the long run it is better to point to the certificate that curl should trust.

Wednesday, September 29, 2010

PHP Curl error numbers (errno:26)

I recently had a hard time finding what the error numbers stand for that are returned when sending an HTTP request with php_curl.
I was looking for error number 26. Error number 26 stands for:

There was a problem reading a local file or an error returned by the read callback.

A complete list of error codes can be found at the cURL project homepage.

Fatal error: Using $this when not in object context

Fatal error: Using $this when not in object context...

This has mostly two different reasons.

1. $this is used outside of a class.

class test{

   var $runtest;

   function test(){
      $this->runtest = 1; // proper usage

   function printThis(){
      echo $this->runtest;

$object = new test();
echo $this->runtest; // fails this not part of an object.
echo $object->runtest; // success 

2. The class method contains $this but was called in a static way.

class TestClass { 
    var $test; 

    function testMethod() { 
        $this->test = 'test'; 

TestClass::testMethod(); // Error 

If the class needs to be used static the other variable or method needs to be called static as well.

class TestClass { 
    var $test; 

    function testMethod() { 
        TestClass::$test = 'test'; 

Mamp and CURL

MAMP comes with the curl libary.

On the MAMP homepage you can find the what liberies of curl and other programs come with witch MAMP version.

MAMP Documentation