Solved

Not sure how to use curl with this particular variable

Posted on 2013-01-17
6
304 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
Comment Utility
Did you try passing value with single quotes?
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 1

Author Comment

by:tjyoung
Comment Utility
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
Comment Utility
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
Comment Utility
Glad it helped.  Var_dump() is really my best friend!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

772 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

10 Experts available now in Live!

Get 1:1 Help Now