Forum Moderators: coopster

Message Too Old, No Replies

cURL Failing

         

SeanF

2:32 pm on May 17, 2020 (gmt 0)

5+ Year Member Top Contributors Of The Month



Hi

I wrote a subscription based business management tool (primarily PHP/MySQL) that my customers use to manage their businesses. The primary business model is for trade show managers.

Over the years, I have successfully used cURL to allow them to display data extract reports on their own web sites. This way, any changes they make on my domain (the business management site) are reflected on their domains (the discrete trade show web sites), these reports might be product lists or customer lists, etc.

I am now trying to export a trade show floor plan but it fails and I can't figure out why: The floor plan is an SVG file that uses a third party code, MapSVG, to associate MySQL data with the SVG elements.

<snip>

The actual SVG file is missing.
If I comment out the MapSVG stuff and echo out the path to the SVG file and echo out all of the MySQL data records, everything look s fine.
Also, the two other smaller image files come in fine.

the cURL code looks like this:
echo "Setting the URL<br/>";
$URL="https://www.example.com/space_availability_map_outside.php";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, "$URL");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
$contents = curl_exec($ch);
curl_close($ch);
echo $contents;

Can anybody advise as to what I'm doing wrong?

Thanks

[edited by: not2easy at 4:25 pm (utc) on May 17, 2020]

SeanF

7:11 pm on May 17, 2020 (gmt 0)

5+ Year Member Top Contributors Of The Month



Thanks for the comment, not2easy.

I'm not sure how you came to the conclusion that the "actual SVG file is missing"

If you looked at the two links that you snipped (and I understand why you did)

The first URL was to the business management domain and the SVG file displayed correctly.

The second link was a simple cURL script which references the first. So, isn't it logical to assume that the SVG file is referenced correctly?

Or does cURL not recognize URLs in referenced files? Or... perhaps cURL does not like PHP "included files?
Can you clarify?

Sorry to be dense.

not2easy

8:01 pm on May 17, 2020 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



The comment that the "actual SVG file is missing" was in your post before it was edited, it was not my conclusion. The part that was removed was originally
You will see that the actual SVG file is missing.
and it was edited because the links needed to be removed. Sorry, but I hope that clarifies your question.

Personally, I don't speak much PHP which is why I did not have a response to your questions. I have had hosts that had disabled cURL which disabled some remote tools I was using. I have run into incompatibility issues between versions of PHP on host servers - but I can't really guess why your script does not work.

I'm sure that with time, others who do have PHP skills will be here to help.

brotherhood of LAN

8:29 pm on May 17, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You need to do error checking. The code you posted as it stands wholly relies on your preferred outcome happening. When it doesn't happen, you have no way of knowing what went wrong. It could be that the host disabled cURL, any remote networking, it could be that the URL is redirecting, is gone, or any number of things.

The best reference is the PHP manual e.g. [php.net...]

Here is a wrapper script I've used in a number of places. The first request to webmasterworld.com works, the second one won't because it's a non-existent domain. When printing out the error code it returns error code 6 which [curl.haxx.se...] says is error number 6 "Couldn't resolve host. The given remote host was not resolved." This tells me the host will never return any valid URL because the domain name system is saying the domain name doesn't exist. As you can see from that page there's almost 100 reasons why you don't get the reply you expect.

The reason for your URL failing could be one of many of the reasons listed on that page. The easiest way to find out is to ask cURL to tell you the reason.


<?php

class curl {

var $hndl; // Handle
var $opts = [
CURLOPT_USERAGENT => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0',
CURLOPT_ENCODING => '',
CURLOPT_HEADERFUNCTION => array('curl', 'head'),
CURLOPT_WRITEFUNCTION => array('curl', 'body'),
];
var $i = []; // Info
var $b = ''; // Response body
var $h = ''; // Response head
var $e = ''; // Last error
var $en = 0; // Last error number

function head($ch, $data) {
$this->h .= $data;
return strlen($data);
}

function body($ch, $data) {
$this->b .= $data;
return strlen($data);
}

function fetch($url, $opts = array(), $getinfo = true) {
$this->h = $this->b = '';
$this->i = [];
$this->hndl = curl_init($url);
curl_setopt_array($this->hndl, $opts);
$bool = curl_exec($this->hndl);
if ($getinfo)
$this->i = curl_getinfo($this->hndl);
$this->e = curl_error($this->hndl);
$this->en = curl_errno($this->hndl);
curl_close($this->hndl);

return $bool;
}
}

$curl = new curl;

// This URL will work
$curl->fetch('https://www.webmasterworld.com', $curl->opts, true);
var_dump($curl->i);
var_dump($curl->h);
var_dump($curl->b);

// This URL won't, why?
$curl->fetch('https://www.dsfjhdsfhdsf.com', $curl->opts, true);
var_dump($curl->i);
var_dump($curl->h);
var_dump($curl->b);
var_dump($curl->e);
var_dump($curl->en);

?>

SeanF

11:09 pm on May 17, 2020 (gmt 0)

5+ Year Member Top Contributors Of The Month



Sorry, not2easy... without the links, the context of my "actual SVG file is missing"comment didn't even make sense to me, LOL

Thanks for the clarification.

SeanF

11:53 am on May 18, 2020 (gmt 0)

5+ Year Member Top Contributors Of The Month



Thanks for the reply "brotherhood of LAN".

I have added the curl_error() function and nothing is returned, (my code is below)
You will notice that I have two URL's in the code. If I uncomment the one without the SVG file and comment out the one with the SVG file, the respective page loads as expected. If I reverse the comment status, the page loads but the SVG file is missing. In neither case is any cURL error displayed.

In the "space_availability_map_outside.php" code there is a line to echo out the full path to the SVG file and that is correct.

Still confused.

<?php
$page_title = "cURL Testing";

// $URL="https://www.[domain_name].com/exhibitors_4_web_2.php?show_id=7";
$URL="https://www.[domain_name].com/space_availability_map_outside.php";

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, "$URL");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
$contents = curl_exec($ch);

echo 'Curl error: ' . curl_error($ch);

curl_close($ch);
echo $contents;

?>

brotherhood of LAN

1:30 pm on May 18, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You don't even see "Curl error:"?

Try putting
ini_set('display_errors', 1)


at the top of the page. If you reach a memory limit or whatnot, then it would fail at curl_exec. Setting it to display errors will at least tell you if something fatal is happening

SeanF

4:42 pm on May 18, 2020 (gmt 0)

5+ Year Member Top Contributors Of The Month



I do see the text "Curl error:" at the top of the page but there is no actual error reported.

I also have the following at the top of both pages, the referenced page as well as the page with the cURL code:
ini_set('display_errors', 1);
error_reporting(-1);

And there are no errors reported there either.

brotherhood of LAN

7:08 pm on May 18, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'd run
echo '<pre>'; print_r(curl_getinfo($ch));


After curl_exec, curl will most definitely report any issues with the fetching part and since you're reporting all errors with PHP that should take care of everything else. Since you're seeing the 'curl error' part it's definitely not silently dieing because of excessive memory usage etc.

SeanF

10:31 am on May 19, 2020 (gmt 0)

5+ Year Member Top Contributors Of The Month



OK... I received the following from the developer of MapSVG... not a show-stopper, just a little more complicated than I was hoping.
You can't load an SVG file from another domain.

MapSVG loads SVG via an ajax call and loading data from other domains via ajax is prohibited in all browsers because of security reasons.

So you need to upload the SVG file to your new domain and then change the "source" option in MapSVG settings object to reflect the new file path.