How to parse response with PHP curl?

2,510 views
Skip to first unread message

ragulka

unread,
Jun 15, 2010, 6:10:49 AM6/15/10
to StreamSend API Discuss
Hello, it seems that I have hit a snag.
I'm trying to create a new filter and get it's ID from the response
headers, but I don't know how to do it.
Here's the code that I'm using:

# CREATE A FILTER
$ch = curl_init();

$headers = array(
'Accept: application/xml', # any data returned should be XML
'Content-Type: application/xml'# any data we send will be XML
);

$xml = '
<filter>
<name>'.$email.' Filter</name>
<blocks>
<block>
<field_name>email_address</field_name>
<rules>
<rule>
<operator>==</operator>
<value>'.$email.'</value>
</rule>
</rules>
</block>
</blocks>
</filter>
';

curl_setopt($ch, CURLOPT_USERPWD,
"kWThJuZvCnpC:cm6gzjDIcaho20ja");
curl_setopt($ch, CURLOPT_URL, "https://app.streamsend.com/
audiences/1/
filters.xml");
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$response = curl_exec($ch);

print_r($response);

echo "<br>";

# get the HTTP status code for our session
$http_status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

# return all options into an associative array
$info = curl_getinfo($ch);

# get the HTTP status code for our session
print_r( $info);

But none of it contains the Filter ID, which should be in the response
Location header, as per documentation:
http://app.streamsend.com/docs/api/classes/FiltersController.html#M00...

ragulka

unread,
Jun 15, 2010, 7:28:07 AM6/15/10
to StreamSend API Discuss
Okay, figured it out myself. Have to set two curl options:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);

Ian Lesperance

unread,
Jun 15, 2010, 1:06:27 PM6/15/10
to streamsend-...@googlegroups.com
I'd also recommend looking at CURLOPT_HEADERFUNCTION.  It allows you to specify a callback function that will be called for each header found in the response.  That function can then parse and store the header by, say, splitting it into a key-value pair and saving it in an associative array for later use.

Ian
Reply all
Reply to author
Forward
0 new messages