PHP problem because or curl

software development

#1

Hi, since the folks at DH simply refused to help me with a script that worked perfectly at my previous server, I beg for your help here.

I have a script that calls products from a brazilian ebay-like site, it’s a legal script and aproved from them.

Problem is that freamhost uses this thing called curl

So the part that is problematic on my script is:

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, “startElement”, “endElement”);
xml_set_character_data_handler($xml_parser, “characterData”);
$fp =
fopen(“http://external url etc etc”)
or die(“Error reading data.”);
while ($data = fread($fp, 4096))
xml_parse($xml_parser, $data, feof($fp))
or die(sprintf(“XML error: %s at line %d”,
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
fclose($fp);
xml_parser_free($xml_parser);

?>

I know the problem is in the fopen part but where exactly do I replace that with curl code? Do I have to remove anything, too?

Thank you so much.

kioshi.org
freewaremac


#2

I recommending reading this wiki article.


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


#3

Thanks for your sugesstion, but that’s the first thing I did and I coudnt have worked this out. I’m no newbie to coding, but thse curl codes are really driving me nuts. I do not know where to replace the fopen lines with curl (since curl requires more than one line), I tried a lot of different places, even what DH support suggested first when they helped me but later even they seem to have given up on helping me with that.

Thanks!

kioshi.org
freewaremac


#4

On the PHP.net website, someone has written an XML parser class that uses cURL. It looks a bit complicated, but it is designed to adapt to a number of possible scenarios:

http://us3.php.net/manual/en/ref.xml.php#59742


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


#5

Thanks for your help, really, I appreciate, but I must confess I am starting to regret having moved to DH.

[color=#CC6600]DREAM85[/color] - get 85 dollars off any yearly plan.
kioshi.org
freewaremac


#6

Kioshi,

I understand how frustrating it can be to have to re-work a script, and I sympathize with you in your situation. One thing I would like to point out, however, is that the PHP option allow_url_fopen is a potential and significant security risk.

DreamHost’s decision to not allow that use of fopen is a good one, and is designed to protect your site, and the sites of others sharing your server, from potential exploitation. More and more hosts are recognizing this, and should you decide to change hosts to one without this restriction, you should be prepared for the fact that they are likely to take the same action as DreamHost at some point in the future. Alternately, a host that leaves this hole open, while it will allow your script to run unmodifed, is exposing your site to a considerable security risk. Given all this, I believe you are much better off to deal with this issue now and modify the script; to retreat to a host that will run it is only delaying the inevitable: either they will ultimately disable it, or you will be hacked. Then you are back to worse than where you started.

If you don’t feel up to modifying the script yourself, or hiring a PHP developer to modify it for you, may I suggest that you contact the authors of your script and see if a newer, more secure version (using cUrl) is available. I think you may find them receptive to your request, as they will encounter this situation more and more often as webhosts tighten their security.

–rlparker


#7

Hi, thanks for the answer. Ive been reading around abour curl and you’re probably right anyway.

I am just so frustrated that this is the only script I wanyted to work that fetces other site’s links and it seems that reaplacing the fopen line with the 4 lines of curl do not work. Not in monetary condition to hire a developer here, unofrtunately.

Thanks for taking your time in writing me this,m you’re very kind.

[color=#CC6600]DREAM85[/color] - get 85 dollars off any yearly plan.
kioshi.org
freewaremac


#8

Could anyone please help me? I am really getting desperate about not being able to run a single php script because DH soes not alow curl.
I’ve read a lot and nothing seems to work… ;(

[color=#CC6600]DREAM85[/color] - get 85 dollars off any yearly plan.
kioshi.org
freewaremac


#9

Please explain exactly what your script is supposed to do, and give me the actual URL you are trying to connect to (so I can see how it is marked up).


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


#10

He Simon,
Thanks again for the attention. This script is supposed to connect to a ebay-liike website and show different products each time. You can see a version of it (as it was in my previous host) in a friend’s site: http://buscaml.com/MLB/Informatica/1648

So you can see at the right side, the products and price and etc. My site (http://shopping.kioshi.org) used to fecth this and looked almost like that (my friend’s site is more alborate but uses the same script) but since I changed to DH I cannot make it work because of curl.

MY script is at http://shopping.kioshi.org/scriptShopping.php

Thanks!

[color=#CC6600]DREAM85[/color] - get 85 dollars off any yearly plan.
kioshi.org
freewaremac


#11

I’m sorry, but that still isn’t enough information for me to go on, because I cannot actually see the PHP script. Please give me a text (txt) version of the script that was working on your previous site.


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


#12

Hi!
So, it’s this part I mentioned in the first post that gives out an error:

It’s all about the fopen function but when I tried changing that to curl I got a ‘not found stream’ error…

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, “startElement”, “endElement”);
xml_set_character_data_handler($xml_parser, “characterData”);
$fp =
fopen(“http://external url etc etc”)
or die(“Error reading data.”);
while ($data = fread($fp, 4096))
xml_parse($xml_parser, $data, feof($fp))
or die(sprintf(“XML error: %s at line %d”,
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
fclose($fp);
xml_parser_free($xml_parser);

?>

[color=#CC6600]DREAM85[/color] - get 85 dollars off any yearly plan.
kioshi.org
freewaremac


#13

I need to see the data you are trying to read, so I need that URL (including any query string information being sent along with it). If I don’t have the URL, I cannot run any tests on a possible solution.


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


#14

I had a similar problem earlier, with getting imagecreatefrom() (requires fopen to be enabled) to work.

try these three functions to replace GET, GET (binary file) and POST

function loadpage($url) {

$ch=curl_init($url);

curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

$page=curl_exec($ch);
curl_close($ch);

return $page;
}

function loadbinary ($url) {

$ch=curl_init($url);

curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_BINARYTRANSFER,1);

$page=curl_exec($ch);
curl_close($ch);

return $page;
}

function postpage($url,$post) {

$ch=curl_init($url);

curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post);

$page=curl_exec($ch);
curl_close($ch);

return $page;
}


#15

Thanks a lot for your answer, shiehb :wink:
So in the script:

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, “startElement”, “endElement”);
xml_set_character_data_handler($xml_parser, “characterData”);
$fp =
fopen(“http://external url etc etc”)
or die(“Error reading data.”);
while ($data = fread($fp, 4096))
xml_parse($xml_parser, $data, feof($fp))
or die(sprintf(“XML error: %s at line %d”,
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
fclose($fp);
xml_parser_free($xml_parser);

I would replace from $fp= until fclose($fp); or just replace the fopen (http://external url) with these lines of code?

Sorry to ask you to babystep me through this, but I’ve tried replacing all thats in the curl Wiki and got no results…

Thanks!

[color=#CC6600]DREAM85[/color] - get 85 dollars off any yearly plan.
kioshi.org
freewaremac


#16

The key to your script appears to be that you want to feed a string, $data, through the XML parser. The $data string is simply the entire contents of the XML file you are trying to read, correct?

As I’ve explained before, I am not proficient in either cURL or XML parsing, but you may be able to try something like this:

<?php $external_xml = "http://external url etc etc"; $ch = curl_init(); $timeout = 5; // set to zero for no timeout curl_setopt ($ch, CURLOPT_URL, $external_url); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $data = curl_exec($ch); curl_close($ch); $xml_parser = xml_parser_create(); xml_set_element_handler($xml_parser, "startElement", "endElement"); xml_set_character_data_handler($xml_parser, "characterData"); xml_parse($xml_parser, $data) or die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); xml_parser_free($xml_parser); ?>--------
Simon Jessey | Keystone Websites
Save $97 on yearly plans with promo code [color=#CC0000]SCJESSEY97[/color]


#17

Simon’s solution should work for you :slight_smile:

though if you are making multiple calls, it would be easier for you to use functions, like the ones I provided…


#18

HI! OUR page (http://popsops.com/keyhole.php) don’t working!
We wrote about this in support, and here is that they have answered:

////////////The problem is with the above
"include(http://nagval.ru/include/pages.php…)" part. This is because
it is a big security risk and causes more server load and traffic to
include a file from another web server. If you really must have this
functionality, please try switching it to use the cURL functions instead.
These are documented here:http://wiki.dreamhost.com/index.php/CURL////////

Help us to do such code, which was worker on this hostinge. we not programmers, tried themselves - a page don’t care does not work!
Do Not know what understand with this code:

<?php // Lnks.ru code $url = "http://" . $HTTP_SERVER_VARS['HTTP_HOST'] . $HTTP_SERVER_VARS['REQUEST_URI']; ini_set('error_reporting','E_ERROR'); include("http://www.lnks.ru/include/?u=188&siteID=119&mpp=20&url=".urlencode($url)."&page=$page&cat=$cat&ua=".urlencode($HTTP_SERVER_VARS['HTTP_USER_AGENT'])); // end Lnks.ru code ?>

Help at earliest convenience! Thank you!


#19

I cannot help you with this because I have no idea what your code is trying to do. Just remember that you cannot use “http://” with include().


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