Solved

Not sure how to use curl with this particular variable

Posted on 2013-01-17
6
315 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 110

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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 110

Expert Comment

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

Featured Post

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

636 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