Search code examples
phpphp-curl

curl_exec pulling wrong HTML vs string passed to curl_init


My curl request string is created programmatically with a base URL from a database and an integer which is the page number. The string for the URL is correct. If I call the curl request with the base URL copied in to the function, it works. If I call it with the programmatically created string (which is identical) the wrong page is called - it appears to be missing one of the parameters from the string (affiliation):

This has been running for years with no issue. The server is PHP 5.6. The string in the database is stored as varchar(500). I have tried TEXT as the type in the database. I have tried various ways of forcing a string in the function. The first $testurl below is copied and pasted from the second $testurl if I get the code to echo it before making the curl call.

//choose one of the $testurls to try
$testurl = 'https://log.concept2.com/challenges/holiday/2019/honorboard/200?country=0&state=&affiliation=1397&sort=distance&page=2';
//In this... $url comes from the database, $gotopage comes from a loop
$testurl = (string)($url . $gotopage);
$c = curl_init($testurl)
$str = curl_exec($c);
curl_close($c);
echo $str; 
die;

The first $testurl returns what I want, a list of people in the same team. The second $testurl returns a list of people belonging to any affiliation.

Edit

var_dump($testurl) gives:

string(134) "https://log.concept2.com/challenges/holiday/2019/honorboard/200?country=0&state=&affiliation=1397&sort=distance&page=2"

The full code is more of a challenge. The $GoToPage is an integer. $url is pulled from a database. The pertinent line is:

$url = $challenge['mb2_Challenges']['challenge_url'];

Where the $challenge is created from a mysqli SELECT... FROM... WHEN... query with SQL. mb2_Challenges is the table, challenge_url is the table.


Solution

  • For those who are as foolish as me, here is the answer:

    The string from the script was urlencoded, so & instead of &. I was in too much of a rush to spot this. It just needed htmlspecialchars_decode.

    Lesson learned: always look at the source!