-
10 Jul 2012 9:26 PM #1
Answered: How to parse an image link from the xml datasets using php then ‘json_encode’ it.
Answered: How to parse an image link from the xml datasets using php then ‘json_encode’ it.
Hi, I have received a xml datasets for a traffic live camera feed.
In order to access the datasets through a browser,
need to put the credentials, username and password, which I was given.
So, how to parse an image link from the xml datasets using php then ‘json_encode’ it.
the xml datasets
parsing the xml using php
parse to sencha javascript store
Then put the fields with reference to the xml datasets
Why am I unable to display the picture though I already put it as img src=imageurl in the sencha view.
Note* I copied the imageurl link and tried to open up with a browser.
It will download a blank image.
-
Best Answer Posted by bluehipy
Joking...
You can package the request with your needed headers and call it from js as you do from php,
Code:Ext.create('Ext.data.Store', { model: 'Entry', proxy: { type: 'ajax', headers:{ "Authorisation": ur_custom_base64_encode(user+':'+passw), "UserAgent":'Foobar' }, url : 'zaXMLprovideer/url/', reader: { type: 'xml', rootPorperty: 'feed entry' } }, });
or you can call a php script file that will do the request to "xml datasets" as you do now, and just return the xml response without json_encode-ing.
zaScript.php
PHP Code:$user='';
$pass='';
...
$xml= new SimpleXmlElement(....);
echo $xml->asXML();
unset($things);
exit();
Code:Ext.create('Ext.data.Store', { model: 'Entry', proxy: { type: 'ajax', url : 'zaScript.php', reader: { type: 'json', rootPorperty: 'feed entry' } }, });
-
10 Jul 2012 10:18 PM #2
In php you will have to return the result of json_encode to the standard output. In your example you just encode the $xml but you don't print / echo the result.
So I guess in the end of your file you need to set:
echo json_encode( $xml );
On the client, in the store definition, the rootProperty is not the 'entry'. There is some xpath `til the entry, like "feed.entry".
Also you can let the js read the xml if you want, ST2 has an xml reader
-
11 Jul 2012 12:41 AM #3
Hi, yup i saw the xml reader before, but i didnt use it because i dont know how to put the credentials.
Need the username and password to access the url xml datasets, referring to my 'parsing the xml using php' picture, 'user' and 'pass'.
I hope you can give an example?
Sorry.
-
11 Jul 2012 2:34 AM #4
I could but I would totally ruin your learning experience

-
11 Jul 2012 2:53 AM #5
Joking...
You can package the request with your needed headers and call it from js as you do from php,
Code:Ext.create('Ext.data.Store', { model: 'Entry', proxy: { type: 'ajax', headers:{ "Authorisation": ur_custom_base64_encode(user+':'+passw), "UserAgent":'Foobar' }, url : 'zaXMLprovideer/url/', reader: { type: 'xml', rootPorperty: 'feed entry' } }, });
or you can call a php script file that will do the request to "xml datasets" as you do now, and just return the xml response without json_encode-ing.
zaScript.php
PHP Code:$user='';
$pass='';
...
$xml= new SimpleXmlElement(....);
echo $xml->asXML();
unset($things);
exit();
Code:Ext.create('Ext.data.Store', { model: 'Entry', proxy: { type: 'ajax', url : 'zaScript.php', reader: { type: 'json', rootPorperty: 'feed entry' } }, });
-
11 Jul 2012 7:51 AM #6
Well, as you see I used there "ur_custom_base64_function" meaning there is no native js base64 encoding, (except mozilla) so you have to define the encoding function, there are a couple on internet.
Regarding the second approach, your model name is wrong in my opinion... "new" is not a good model name... model names shoud not be reserved words and should be uppercase. (but I don t think it is necesarily the issue in your case, fix it anyway)
The entry nodes have: id, title, summary, author, content, category... so there "title" and "updated" which you are specifying in model, that s fine.
On the view, again, the "new" naming thing is not inspired... change it maybe.
Also the code excerpt is not relevant to the cause, you should expose the listing part. and the store part.
Also check if php service is returning a valid xml as you expect.
-
11 Jul 2012 6:49 PM #7
Ouh ok. I will change the name.
Another thing, i tried to return the xml response without json_encode-ing in the php using your example and in the console it will say 'You're trying to decode an invalid JSON String'
-
12 Jul 2012 1:26 AM #8
Maybe you kept the reader type to 'json' ?
-
25 Jul 2012 8:03 PM #9
Sorry for the late reply. I solve it already.
i forgot to put this in the php.
header('Content-type: application/xml');
before the
echo $xml->asXML();


Reply With Quote