Solved

Not sure how to use curl with this particular variable

Posted on 2013-01-17
6
307 Views
Last Modified: 2013-01-17
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

0
Comment
Question by:tjyoung
6 Comments
 
LVL 12

Expert Comment

by:sivagnanam chandrakanth
ID: 38786193
Did you try passing value with single quotes?
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 38786237
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
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 38786864
Use var_dump() to print out the contents of $result and post that here, please.

Try running this script.  I do not think that "3.0" is the problem.
http://www.laprbass.com/RAY_urlencode.php?i=3.0

<?php // RAY_urlencode.php
error_reporting(E_ALL);
echo "<pre>" . PHP_EOL;


// READ CLIENT INPUT AND ECHO IT WITH URLENCODE
// MAN PAGE HERE: http://php.net/manual/en/function.urlencode.php


if (!empty($_GET["i"]))
{
    $i = urlencode($_GET["i"]);
    echo PHP_EOL . htmlentities($_GET["i"]) . ' URLENCODED = ' . htmlentities($i);
    echo PHP_EOL;
}

$form = <<<FORM
ENTER A STRING TO SEE THE URLENCODE() VERSION
<form>
<input name="i" />
<input type="submit" />
</form>
FORM;

echo $form;

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:tjyoung
ID: 38787470
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
0
 
LVL 1

Author Closing Comment

by:tjyoung
ID: 38788150
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.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 38789717
Glad it helped.  Var_dump() is really my best friend!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now