PHP Kohana parse simple XML - php

Having some problem parsing some XML in an application I am building. The format is as follows
<taskResponse statusCode="200">
<session-states>
<min-state>dfgdgdgd</min-state>
<max-state>dgdfgd</max-state>
<session-info>dgffdgd</session-info>
<project-id>B19DEDCC11D4E0EFC000EB9495D0F44F</project-id>
</session-states>
</taskResponse>
Attempting to use the following code to parse out the session-state value
public function parse_session_state($xml){
echo $xml;
$xml = new SimpleXMLElement($xml);
echo $xml->session-states[0]->min-state;
die();
}
but I get an error message
ErrorException [ Parse Error ]: syntax error, unexpected '[', expecting ',' or ';'

This seems to work:
public function parse_session_state($xml){
$xml = new SimpleXMLElement($xml);
echo $xml->{'session-states'}->{'min-state'};
die();
}

Related

New object with name in variable

I am trying to initiate an object with a dynamic path. I have the variable $model with the model name.
$model = "foo";
$class = new \Path\To\$model();
I get the error
Parse error: syntax error, unexpected '$model' (T_VARIABLE), expecting identifier (T_STRING)
If I try $class = new \Path\To\{$model}();
I get the error
Parse error: syntax error, unexpected '{', expecting identifier (T_STRING)
When I try
namespace \App\Models
$class = new $model();
I get the error Class 'foo' not found
When I try $class = new \Path\To\foo(); it works.
Any ideas?
Try:
$class = "\Path\To\foo";
$object = new $class();
Or:
use Path\To\foo;
$class = foo::class;
$object = new $class();
You can store path in variable:
$path = "\Path\To\\";
and then generate class name like this:
$className = $path.$model;
$class = new $className();

syntax error, unexpected T_OBJECT_OPERATOR? Facebook

I am practicing with facebook PHP api
when i am trying to make a login function
here comes out this warnning
syntax error, unexpected T_OBJECT_OPERATOR in /home/u528851895/public_html/Desktap/facebook-php-sdk-v4-4.0-dev/src/Facebook/FacebookResponse.php on line 137
but it's ok when i use mamp localhost
here's line 136~138 in FacebookResponse.php:
public function getGraphObject($type = 'Facebook\GraphObject') {
return (new GraphObject($this->responseData))->cast($type);
}
Upgrade your php version or do something like this
public function getGraphObject($type = 'Facebook\GraphObject') {
$obj = new GraphObject($this->responseData);
return $obj->cast($type);
}

domDocument loadHTML getElementsByTagName on a non-object issue

Im new to PHP, so im a little stuck in this code ex.
if U look at this Link there is a table, im trying to get only that table out and I know its the first table and its comming on source code line 1065.
But i get this error
bool(false)
Fatal error: Call to a member function getElementsByTagName() on a non-object in /get.php on line 23
I have this code, hope someone can guide me. (line 23 is the $rows..line)
<?php
$pulje = '163532';
$url = "http://www.dbu.dk/turneringer_og_resultater/resultatsoegning/position.aspx?poolid=$pulje";
// enable user error handling
var_dump(libxml_use_internal_errors(true));
// parse the html into DOMDoc.
$dom = new domDocument();
$dom->recover = true;
$dom->strictErrorChecking = false;
$dom->loadHTML($url);
$tables = $dom->getElementsByTagName('table');
$rows = $tables->item(count($tables-1))->getElementsByTagName('tr');
$array = array();
foreach($rows as $row){
$cols = $row->getElementsByTagName('td');
echo $array[] = $cols;
}
?>
....UPDATE....
I updated the code frem loadHTML to loadHTMLFile and then i now get this error
bool(false) Catchable fatal error: Object of class DOMNodeList could not be converted to string in /get.php on line 28
Line 28 is the echo $array....
You should use DOMDocument::loadHTMLFile instead of DOMDocument::loadHTML
DOMDocument::loadHTML : Load HTML from a string
DOMDocument::loadHTMLFile : Load HTML from a file
http://php.net/manual/en/domdocument.loadhtml.php
http://php.net/manual/en/domdocument.loadhtmlfile.php

ckfinder integration with ckeditor

This is my integration code:
My extranet is in root/include/ckeditor and root/include/ckfinder
$baseUrl = 'https://extranet.com/crm/include/;
$baseUrl = '/userfiles/';
$baseDir = '/crm/include/userfiles/;
I am trying to do the configuration thats all.
I am trying to integrated with ckeditor but i am getting all sorts of error. Can anyone tell me where iam doing wrong.
It was not possible to properly load the XML response from the web server.
XML Parsing Error: junk after document element Location: https://extranet.zeald.com/crm_snapshot/include/ckfinder/ckfinder.html?CKEditor=message&CKEditorFuncNum=2&langCode=en Line Number 2, Column 1:Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /mnt/clusternfs2/extranet/crm_snapshot/include/ckfinder/core/connector/php/php4/Utils/Misc.php on line 54
^
Raw response from the server:
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /mnt/clusternfs2/extranet/crm_snapshot/include/ckfinder/core/connector/php/php4/Utils/Misc.php on line 54
Edit:
looking into the Misc.php here the code which is giving the error
public static function encodeURIComponent($str)
{
$revert = array('%21'=>'!', '%2A'=>'*', '%27'=>"'", '%28'=>'(', '%29'=>')');
return strtr(rawurlencode($str), $revert);
}
I have got php version 4.3.10 running and I read the ckfinder documentation that version2.2.1 is being supported by php4+.
Edit:
I have removed this code and it seems to be working :
/* public static function encodeURIComponent($str)
{
$revert = array('%21'=>'!', '%2A'=>'*', '%27'=>"'", '%28'=>'(', '%29'=>')');
return strtr(rawurlencode($str), $revert);
} */
Is there a way to make this work.
Hope someone can help me out.

PHP object property declaration error

I'm having a bit of trouble defining a property in a PHP class I'm creating.
<?php
class news_parser {
public $var1 = Array();
function contents($parser, $data) {
printf($data);
}
function start_tag($parser, $data, $attribs) {
printf($data);
}
function end_tag($parser, $data) {
printf($data);
}
function parse() {
if(!$file = fopen("http://services.digg.com/2.0/story.getTopNews?type=rss&topic=technology", "r"))
die("Error opening file");
$data = fread($file, 80000);
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, array($this, "start_tag"), array($this, "end_tag"));
xml_set_character_data_handler($xml_parser, array($this, "contents"));
if(!xml_parse($xml_parser, $data, feof($fh)))
die("Error on line " . xml_get_current_line_number($xml_parser));
xml_parser_free($xml_parser);
fclose($fh);
}
}
$digg_parser = new news_parser();
$digg_parser->parse();
echo phpversion();
?>
Produces the following error:
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /homepages/8/d335242830/htdocs/caseyflynn/php/display_formatted_RSS_feed.php on line 3
As far as I can tell I have the correct syntax. My server is running PHP 4.5. Any ideas?
My server is running PHP 4.5
This is your problem: PHP 4 doesn't know the public keyword - along with a heap of other OOP features.
As #konforce says, in the code you show, you can simply switch to using the var keyword. But the best thing would really be to switch to PHP 5. PHP 4's time is really, really over.

Categories