November 11th, 2012, 07:44 PM
-
Parsing cURL return
Hello ladies and gentlemen. This is my first post to this forum. I am learning PHP right now and as of today, I am terrible at it. I am brand new. I am familiar with another proprietary language called netlinx, which is somewhat similar in many ways, but the string manipulation and all is totally different.
At any rate, I am working on a project where I will ultimately populate a database with all sorts of product data for the backend of an ecommerce site, but I am in the process right now of simply figuring out how PHP works. The manual and examples are great, if you sort of have a rough idea what you are looking for, but in this situation, I don't know really what direction to go in.
I have some data that comes back in response to a cURL exec (see below). I don't know what to do to sort of categorize it. It is basically some category numbers, followed by a name and attribute. Eventually, I will try to cram this into a database table, but right now I am just trying to parse the data and don't know where to start. In the language I normally work with, there is a function called REMOVE_STRING (Variable,Delimiter,Start Position) and I would for loop through all this stuff. I have seen the EXPLODE function, but all the examples are very simple with single instances of single character delimiters. What direction do I need to go in to start cramming this into a multidimensional array, and then eventually into database tables? This is an example of basically how all the data will be setup....just different field tags when it turns to say, manufacturer, quantity on hand, etc...
In this window it posts very structured, but as it come into the webpage, it looks like one crammed long string.
DATA SAMPLE
<NewDataSet>
<Table>
<CATID>0</CATID>
<CATDES>UNASSIGNED </CATDES>
<ATTR1>locked </ATTR1>
<ATTR2 xml:space="preserve"> </ATTR2>
<ATTR3 xml:space="preserve"> </ATTR3>
<ATTR4 xml:space="preserve"> </ATTR4>
<ATTR5 xml:space="preserve"> </ATTR5>
<ATTR6 xml:space="preserve"> </ATTR6>
<ATTR7 xml:space="preserve"> </ATTR7>
<ATTR8 xml:space="preserve"> </ATTR8>
<ATTR9 xml:space="preserve"> </ATTR9>
<ATTR0 xml:space="preserve"> </ATTR0>
</Table>
<Table>
<CATID>1</CATID>
<CATDES>ACCESSORIES MISCELLANEOUS </CATDES>
<ATTR1>Type </ATTR1>
<ATTR2>Model or Style </ATTR2>
<ATTR3>Color </ATTR3>
<ATTR4>Size </ATTR4>
<ATTR5>Material </ATTR5>
<ATTR6>Gauge </ATTR6>
<ATTR7>Mount </ATTR7>
<ATTR8>Battery Config </ATTR8>
<ATTR9>Height </ATTR9>
<ATTR0>Model Fit </ATTR0>
</Table>
<Table>
<CATID>2</CATID>
<CATDES>AIR GUNS </CATDES>
<ATTR1>Type </ATTR1>
<ATTR2>Action </ATTR2>
<ATTR3>Caliber </ATTR3>
<ATTR4>Capacity </ATTR4>
<ATTR5>Finish </ATTR5>
<ATTR6>Barrel Length </ATTR6>
<ATTR7>Sights </ATTR7>
<ATTR8>Weight </ATTR8>
<ATTR9>Stock </ATTR9>
<ATTR0>Length </ATTR0>
</Table>
<Table>
<CATID>3</CATID>
<CATDES>ATV ACCESSORIES </CATDES>
<ATTR1>Type </ATTR1>
<ATTR2>Model </ATTR2>
<ATTR3>Color </ATTR3>
<ATTR4>Material </ATTR4>
<ATTR5>Size </ATTR5>
<ATTR6>Additional Info </ATTR6>
<ATTR7>Hardware Included </ATTR7>
<ATTR8>Vinyl Type </ATTR8>
<ATTR9>Weight Capacity </ATTR9>
<ATTR0>Hitch Type </ATTR0>
</Table>
<Table>
<CATID>4</CATID>
<CATDES>BLANK ROUNDS </CATDES>
<ATTR1>Type </ATTR1>
<ATTR2>Caliber
November 11th, 2012, 08:17 PM
-
FYI that data comes in from a URL not a file.
November 11th, 2012, 09:11 PM
-
It appears SIMPLE XML functions are the key, but my variable
$Check_Value is always a boolean; in an example video I found online, it comes in as a string...can't figure out why mine stays boolean.
I really only want to populate a variable with the CURL_EXEC material, not output it to the browser. I just want it to fill with XML data....
$ch = curl_init();
$URL_VALUE="URL THAT WORKS BUT FORUM WONT ALLOW POSTING"
curl_setopt($ch, CURLOPT_URL,$URL_VALUE );
curl_setopt($ch, CURLOPT_HEADER, 0);
$Check_Value=curl_exec($ch);
curl_close($ch);
November 11th, 2012, 09:43 PM
-
cURL is sort of special in that the default value for CURLOPT_RETURNTRANSFER is retarded. In order to get useful output from curl_exec, change it to true:
PHP Code:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
PHP FAQ
Originally Posted by Spad
Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around