Sunday, August 21, 2011

My answer to "Lampcms Write API"

This is my answer to a Question on Support site for LampCMS project


TO use WRITE API you need to make POST request to API and also pass basic authentication in headers.

This is how to do this from php using curl: (replace with your own existing question id, replace with your own url, use your own username and password)

$aInputs = array(
'a' => 'addanswer',
'qbody' => 'bbb body of answer here again!', 
'qid' => '631');


try{
        $oCurl = new \Lampcms\Curl();
        $oCurl->setBasicAuth('user2', 'test123');

        $oCurl->getDocument('http://127.0.0.5:88/api/api.php', null, null, array('formVars' => $aInputs, 'redirect' => 4));

        $body = $oCurl->getResponseBody();

        echo ' body: '.$body;

} catch (\Exception $e){

        $info = $oCurl->getCurlInfo();
        echo 'INFO: '.print_r($info, 1);

        if($e instanceof \Lampcms\HttpRedirectException){
                $newUrl = $e->getNewURI();
                get_page($newUrl, $info['url']);
        }
        $s = 'E: '.$e->getMessage().' in '.$e->getFile().' on '.$e->getLine();
        exit($s);
}


No comments:

Post a Comment