Link to home
Start Free TrialLog in
Avatar of tjyoung
tjyoung

asked on

Not sure how to use curl with this particular variable

Hi,
I have a switch statement and in order for me to get a response, I have to include:
schemaVersion=3.0 in the request when the command is 'trims'. I don't have to when its 'makes' or 'models'. Just trims. The makes and models work just fine. But once I include the schemaVersion as a parameter in the url, it dies. Not sure I'm doing it right at all?

You can see my attempt below. It fails and the message is simply the foreach statement is crapping out.

I'm paranoid about the period in the 3.0, not sure if that is it?

case trims:

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS,"command=$command&year=$year&make=$make&model=$model&account=$account&key=$key&schemaVersion=3.0");
curl_setopt($ch,CURLOPT_RETURNTRANSFER , 1 );

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

$xml = new SimpleXMLElement($result);

  foreach ($xml->Response->Trims->Trim as $trim) {
	$data[] = $trim;
}
break;

Open in new window

Avatar of sivagnanam chandrakanth
sivagnanam chandrakanth
Flag of India image

Did you try passing value with single quotes?
Avatar of Dave Baldwin
I believe it is the 3.0.  Even with curl, you should be 'urlencoding' your query parameters.  I was just working on a Paypal problem and everything passed to curl gets urlencoded there.

See examples here: http://us3.php.net/manual/en/function.urlencode.php
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tjyoung
tjyoung

ASKER

Your right Ray, I'm not having an issue with 3.0.
I did what you suggested and it returned:

string(287) "<?xml version="1.0"?>
<cbb xmlns="http://xml.blkbk.com/vehicle/3/0" schemaVersion="3.0">
<response>
<dataVersion>20130114</dataVersion>
<trims>
<trim id="213">GL</trim>
<trim id="217">GLS</trim>
<trim id="267">L</trim>
<trim id="281">Limited</trim>
</trims>
</response>
</cbb>

Open in new window


I think where I'm failing is how to iterate through. My foreach loop doesn't work:

$xml = new SimpleXMLElement($result);

  foreach ($xml->Response->Trims->Trim as $trim) {
      $data[] = $trim;
}

When I put that part of the code back in, the error is:
Warning: Invalid argument supplied for foreach() in /home/halifaxb/public_html/trade/blackbook.php on line 90
Avatar of tjyoung

ASKER

Hi, the var_dump helped me realize what the issue was: urlencode wasnt causing a problem, what was is the response and trims/trim were being returned in lowercase where makes, models and years were coming back capitalized for some bizarre reason.

I adjusted that and voila.
Glad it helped.  Var_dump() is really my best friend!