Tag: php

  • composer error /usr/bin/env: php: No such file or directory

    Error when running composer:
    composer
    /usr/bin/env: php: No such file or directory

    Probably, php-cli is not installed. Try:
    Centos:
    yum install php5-cli
    Ubuntu:
    sudo apt-get install php5-cli

    More probably, php5-cli is installed but not found in $PATH. Find out where is the php/bin folder and try:
    export PATH=”$PATH:/path/to/php/bin”

  • PHP cURL error code 60 SSL certificate problem self signed certificate in certificate chain

    Error: When using PHP curl_exec() to a site with https and self signed certificate (which should be likely a staging site, not a production site)

    Error code 60 SSL certificate problem: self signed certificate in certificate chain

    Solution:

    Since it is staging environment, we can completely ignore the certificate verification:

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

  • PHP cURL error code 7 failed to connect

    Error code 7 (CURLE_COULDNT_CONNECT) when calling curl_exec()

    $ch = curl_init("http://www.google.com");    
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    $data = curl_exec($ch);
    print($data); // false
    print(curl_error($ch)); // Failed to connect to www.google.com 80
    print(curl_errno($ch)); // 7
    

    Solution:

    1. Check your internet connection

    2. Check your firewall

    3. Add your proxy if you are connecting through proxy:

    $proxy = “192.168.1.1:8080”;
    curl_setopt($ch, CURLOPT_PROXY, $proxy);

  • MongoDB PHP Exception: Can’t connect over SSL, is mongod running with SSL?

    Error:Connect to MongoClient using SSL = true

    $mc = new MongoClient("mongodb://server1", array("ssl" => true));

    Cause: Your mongod instance is running but not properly configured with ssl.

  • Mongo cannot run command count(): failed on : [primary shard]

    Error: When using MongoDB sharded cluster 2.6 and php mongo driver,  db.collection.count() contains an $in operator would raise a BadValue error if using assoc array or bad numerical indexed array:

    array(‘apple’->’iphone’, ‘nokia’->’winphone’)

    array(0->’a’, ‘2’->b)

    Solution:

    In PHP, use array_values($array) to get the numerical indexed array as input for $in query

  • Mongo can’t canonicalize query: BadValue $in needs an array

    Error: From MongoDB 2.6, $in need an numerical indexed array. Associative array or malformed numerical indexed array like these will raise BadValue error:

    array(‘apple’->’iphone’, ‘nokia’->’winphone’)

    array(0->’a’, ‘2’->b)

    Solution:

    In PHP, use array_values($array) to get the numerical indexed array as input for $in query

  • Modify private variable using PHP ReflectionProperty

    class Foo
    {
    private $bar = ‘0’;

    public function bar()
    {
    return $this->bar;
    }
    }

    $foo = new Foo;
    $attribute = new ReflectionProperty($foo, ‘bar’);
    $attribute->setAccessible(true);
    $attribute->setValue($foo, ‘1’);
    var_dump($foo->bar());