curl-and-php
RE: How to a parse result using curl?
Date: Tue, 26 Feb 2002 12:30:37 +0200
At 09:23 PM 2/24/2002 -0800, Dan Brown wrote:
>This does separate the value s but it assigns them new names? I need to
>make the value s variables that I can plug in anywhere I like. And I need
>to suppress the output of what s returned and only display what I put in
>the table.
To take better advantage of php have a go with these ideas:
<?php
# This will suppress the output and place it
# into $result instead.
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
# For this example, though, I'm using:
# $result = "item1=val1<BR>item2=val2<BR>item3=val3";
# The following splits $result into an array called $items,
# with the '<BR>' and '=' being the delimiters.
$items = preg_split("/<BR>|=/", $result);
# Then create a new array ($results) which
# will contain the items as key=value pairs,
# which you can access anywhere in your script
# like this:
# $results->item1
# $results->item2
# etc.
while ($key=array_shift($items))
$results[$key] = array_shift($items);
# If you want to go a step further and create
# new variables whose names are those of the
# keys, you add the following:
extract ($results, EXTR_OVERWRITE);
# This will create the variables item1, item2, etc.
# though you must be careful using this function
# since you may already have existing variables
# of the name you are extracting in your program.
# The option EXTR_OVERWRITE will cause extract()
# to overwrite any existing variables of the same
# name. There are other options you can use with this
# function, see the php manual page for extract()
# But the (minor) advantage is that you can now
# write:
print ($item1);
# and you should get $val1
?>
Received on 2002-02-26