Need help with curl

software development

#1

Hi, I am using the myWeather plugin for wordpress and it uses the
file_get_contents which I need to convert to curl…can anyone tell me where to start, the wiki wasn’t that helpful for a complete braindead blonde today!

here is the code i need to change

$file=file_get_contents($fdata);
$xml_parser = xml_parser_create();
xml_set_character_data_handler($xml_parser, “data”);
$xml=xml_parse($xml_parser,$file);


#2

It’s actually pretty straightforward. Only the first line needs to be replaced with the cURL version:$ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, $fdata); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5); $file = curl_exec($ch); curl_close($ch); $xml_parser = xml_parser_create(); xml_set_character_data_handler($xml_parser, "data"); $xml=xml_parse($xml_parser,$file);--------
si-blog | Keystone Websites
Save $97 on yearly plans with promo code [color=#CC0000]SCJESSEY97[/color]


#3

I am actually trying to get the weather of two different cities on my blog, but only one shows up at a time…can it be because of this line?

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);


#4

I don’t believe so. Setting CURLOPT_RETURNTRANSFER to 1 (or TRUE) tells cURL to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly. You should be able to check the output of the cURL call by adding this:curl_close($ch); [color=#CC0000][b]echo $file;[/b][/color] $xml_parser = xml_parser_create();If it doesn’t contain all the information you are expecting, there is something wrong with the URL (or its query string) you are scraping.


si-blog | Keystone Websites
Save $97 on yearly plans with promo code [color=#CC0000]SCJESSEY97[/color]


#5

I get

Fatal error: Call to undefined function url_close() in


#6

never mind on that one…I inadvertantly removed the c in curl


#7

now I am getting

Warning: curl_close(): 88 is not a valid cURL handle resource in /home/.sidestep/mywebid/mysite.com/wp-content/plugins/myweather/myweather.php on line 34
en_US
MEDIUM
F mi mph in in Tucson, AZ 11:18 AM 31.57 -111.29 5:20 AM 7:32 PM -7 6/14/07 11:00 AM MST Green Valley, AZ 95 91 Sunny 32 29.78 rising 2 N/A 80 ENE 10 10.0 10 Very High 31 29 New

Sunny


#8

It should work. Check your script for errors, and check for any redefinition or changes to the variable $ch.


si-blog | Keystone Websites
Save $97 on yearly plans with promo code [color=#CC0000]SCJESSEY97[/color]


#9

here is a screenshot of the end result:
http://www.protoolstraining.com/whatitsdoing.jpg

as you can see, it’s getting all of the information, but upon displaying the second city, it displays the number that the icon uses instead of the icon itself, and it displays the current time instead of the city name.

I tried two variations and locations in the side bar as illustrated with the same results.

here is how I added the php code to sidebar.php

<?php weather("USAZ0159","F");?> <?php weather("USAZ0247","F");?>

here is the code from the script (for some reason, I cannot post the code here, not even as a quote.)

http://www.protoolstraining.com/weatherscript.html

I’ve looked and I just can’t see it.

thanks
barbara


#10

Hmmm.

I recommend contact the author of the plug-in and asking him to provide you with a cURL alternative.


si-blog | Keystone Websites
Save $97 on yearly plans with promo code [color=#CC0000]SCJESSEY97[/color]


#11

I was using an editor before, and chose to use the wordpress plugin editor

it will let me make changes, but seems that it won’t let me make the change to

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $fdata);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
$file = curl_exec($ch);
curl_close($ch);
$xml_parser = xml_parser_create();
xml_set_character_data_handler($xml_parser, “data”);
$xml=xml_parse($xml_parser,$file);

it gives me the dreaded:

"Service Temporarily Unavailable

The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later."

If I try to make other changes, it lets me…does anyone know why the editor won’t allow this change?

I realized when I had made the changes earlier, the plugin page showed all of the code rather than the name, then the description, then activation, and I get a fatal error if I upload with the above change and try to activate.


#12

and upon closer examination, here is the line of code that causes the error

$file = curl_exec($ch);


#13

The error is there because that is when the cURL command is actually executed. Everything before than line is just “setting it up” - so the error is most likely to occur earlier.


si-blog | Keystone Websites
Save $97 on yearly plans with promo code [color=#CC0000]SCJESSEY97[/color]


#14

Hi Simon

Perhaps the error is just moot. I have gone through the code line for line and cannot figure out how to get it to work. The author’s site is in spanish…and, although I am from AZ, I don’t speak the language…so I doubt asking him would help.

the script works, other than with curl, I have seen many sites with it working. The only two other scripts I found seem a little harder to get to work with curl, and I don’t think either of those works with multiple cities.

Thanks for your help…sorry to be a burden. I will just have one city up with weather…it’s only a blog for a friend moving away.

I will try to eventually make it work, and if I do, I will post the code here and in the wiki.

again, thanks
barbara