Solved

Not sure how to use curl with this particular variable

Posted on 2013-01-17
6
314 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Horizontal Full Calendar using php 5 43
Can't Find the Homepage on my site 4 46
integrated two Separate file . 3 30
Position image fpdf 4 18
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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 count occurrences of each item in an array.

751 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