Em Emertana
Em Emertana

Reputation: 11

How to parse Curl response?

I am able to send the GET request and receive the response at following line.

 $curl_resp = curl_exec($curl);

I used the following to parse the response, but it does not work, I have manually set some values to $curl_resp but still not sure how to access the value of each tag of the xml separately.

$xml = simplexml_load_string($curl_resp);

NOTE: I recevice the actual xml but cant parse it, (I need to get each tag's value separately in a variable)

Code:

<?php

   $service_url = ' The Url goes here';
   $curl = curl_init($service_url);
   $curl_post_data = array(
        "PASSWORD" => 'pass',
        "USERNAME" => 'username'
        );
   curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
   curl_setopt($curl, CURLOPT_POST, true);
   curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
   $curl_resp = curl_exec($curl);
   curl_close($curl);

Upvotes: 0

Views: 3028

Answers (3)

gnomical
gnomical

Reputation: 5043

if the CURLOPT_RETURNTRANSFER option is not set then your $curl_resp will just return true/false.

if it is set you may be returning false or a poorly formed xml string. If you post more code or the actual curl response text we may be able to provide more info.

EDIT:

upon reading the code looks like you are assigning the response text to $curl_response instead of $curl_resp

Upvotes: 1

David Harris
David Harris

Reputation: 2707

Your variable $curl_response is different than $curl_resp (what you're trying to parse)

You can access the value of each tag just like any other array.

Upvotes: 2

Avio
Avio

Reputation: 29

$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, "your url");    
curl_setopt($ch, CURLOPT_USERAGENT, 'Googlebot/2.1 (+http://www.google.com/bot.html)');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);

Upvotes: -1

Related Questions