PHP curl issue - error if using a variable as the URL, but a string works fine
<p>I'm having a very strange curl issue within php. When I pass in a URL as a variable, the request will return a 400 error saying "Bad Request - missing offer id cookie".</p>

<p>But if hard-code the URL into the code instead of passing a variable, it works fine!</p>

<p>Here is my code -</p>

<pre><code>            function resolveURL($url) {

                $ch = curl_init("$url");  
                echo var_dump("$url");

                curl_setopt($ch, CURLOPT_HEADER, 1); 
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); 
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
                $results = curl_exec($ch); 
                curl_close($ch); 

                echo var_dump($results);
            }
</code></pre>

<p>The var_dump of $url return - <code>string(104) "http://click.linksynergy.com/fs-bin/click?id=SN4dmHdm/i8&amp;offerid=462146.45&amp;type=3&amp;subid=0"</code></p>

<p>The var_dump of $results returns </p>

<p><code>string(319) "HTTP/1.1 400 Bad Request Server: Apache-Coyote/1.1 Content-Length: 187 Date: Thu, 22 Dec 2016 14:14:38 GMT Connection: close Missing offer id cookie "</code></p>

<p>But if I make one change to the code above - </p>

<p><code>$ch = curl_init("http://click.linksynergy.com/fs-bin/click?id=SN4dmHdm/i8&amp;offerid=462146.45&amp;type=3&amp;subid=0");</code></p>

<p>Then the $results returns the full data I expect-</p>

<p><code>string(1125) "HTTP/1.1 302 Found Server: Apache-Coyote/1.1 Set-Cookie: lsn_statp=SSUMBwoAAAAlJc5KdQpJiw%3D%3D; Domain=.linksynergy.com; Expires=Wed, 17-Dec-2036 14:16:14 GMT; Path=/ Set-Cookie: rmuid=e431e402-a482-4611-a78a-71aa74f859f5; Domain=.linksynergy.com; Expires=Fri, 22-Dec-2017 14:16:14 GMT; Path=/ Set-Cookie: lsn_qstring=SN4dmHdm%2Fi8%3A462146%3A; Domain=.linksynergy.com; Expires=Fri, 23-Dec-2016 14:16:14 GMT; Path=/ Set-Cookie: lsn_track=UmFuZG9tSVZDI%2Bw8hSY%2BdfkMlwCUwBvtWwSdxm0SOqHNgxhyQqiQeQ0nwzwxomWBIzUbWVW%2Ft7lFTQ3k3hZXdQ%3D%3D; Domain=.linksynergy.com; Expires=Sun, 20-Dec-2026 14:16:14 GMT; Path=/ Set-Cookie: lsclick_mid38366="2016-12-22 14:16:14.418|SN4dmHdm_i8-R8CHpEfhXw637RnHirbegw"; Version=1; Domain=.linksynergy.com; Max-Age=63072000; Expires=Sat, 22-Dec-2018 14:16:14 GMT; Path=/ P3P: policyref="/w3c/p3p.xml", CP="NOI DSP COR CURa ADMa DEVa OUR BUS STA" Expires: Thu, 01 Jan 1970 00:00:00 GMT Date: Thu, 22 Dec 2016 14:16:14 GMT Cache-Control: no-cache Pragma: no-cache Location: http://www.daskeyboard.com/?siteID=SN4dmHdm_i8-R8CHpEfhXw637RnHirbegw Content-Length: 0 Connection: close "</code></p>

<p>Why is this the case? I figure for some reason, everything after the first <code>&amp;</code> is getting stripped when I leave the URL as a variable, but I can't explain why.</p>

<p>I have tried running the $url through <code>urlencode()</code>, <code>htmlentities()</code>, and <code>htmlspecialchars()</code> but I still get the same results. I have tried passing in <code>$url</code> instead of <code>"$url"</code>, and nothing fixes the issue.</p>

<p>UPDATE: When I do a <code>echo var_dump(curl_getinfo($ch));</code> after setting the URL, it looks like the length of the string is somehow longer than it should be when passing in <code>$url</code>, but is the correct length when using the hard-coded string. Here are the results -</p>

<pre><code>//$url
`array(21) { ["url"]=&gt; string(104) "http://click.linksynergy.com/fs-bin/click?id=SN4dmHdm/i8&amp;offerid=462146.45&amp;type=3&amp;subid=0" ["content_type"]=&gt; NULL ["http_code"]=&gt; int(0) ["header_size"]=&gt; int(0) ["request_size"]=&gt; int(0) ["filetime"]=&gt; int(0) ["ssl_verify_result"]=&gt; int(0) ["redirect_count"]=&gt; int(0) ["total_time"]=&gt; float(0) ["namelookup_time"]=&gt; float(0) ["connect_time"]=&gt; float(0) ["pretransfer_time"]=&gt; float(0) ["size_upload"]=&gt; float(0) ["size_download"]=&gt; float(0) ["speed_download"]=&gt; float(0) ["speed_upload"]=&gt; float(0) ["download_content_length"]=&gt; float(-1) ["upload_content_length"]=&gt; float(-1) ["starttransfer_time"]=&gt; float(0) ["redirect_time"]=&gt; float(0) ["certinfo"]=&gt; array(0) { } } `

//hard-coded
`array(21) { ["url"]=&gt; string(89) "http://click.linksynergy.com/fs-bin/click?id=SN4dmHdm/i8&amp;offerid=462146.45&amp;type=3&amp;subid=0" ["content_type"]=&gt; NULL ["http_code"]=&gt; int(0) ["header_size"]=&gt; int(0) ["request_size"]=&gt; int(0) ["filetime"]=&gt; int(0) ["ssl_verify_result"]=&gt; int(0) ["redirect_count"]=&gt; int(0) ["total_time"]=&gt; float(0) ["namelookup_time"]=&gt; float(0) ["connect_time"]=&gt; float(0) ["pretransfer_time"]=&gt; float(0) ["size_upload"]=&gt; float(0) ["size_download"]=&gt; float(0) ["speed_download"]=&gt; float(0) ["speed_upload"]=&gt; float(0) ["download_content_length"]=&gt; float(-1) ["upload_content_length"]=&gt; float(-1) ["starttransfer_time"]=&gt; float(0) ["redirect_time"]=&gt; float(0) ["certinfo"]=&gt; array(0) { } } `
</code></pre>

<p>Can anyone explain where the extra 15 characters are coming from when I use the variable?</p>

<p><strong>Update:</strong> By doing a <code>str_split</code> on <code>$url</code>, I have found that for some reason, there is a hidden '#038;' in the variable after every <code>&amp;</code> that does not show when I echo the string. Below is what the array returns -</p>

<pre><code>[56]=&gt; string(1) "&amp;" [57]=&gt; string(1) "#" [58]=&gt; string(1) "0" [59]=&gt; string(1) "3" [60]=&gt; string(1) "8" [61]=&gt; string(1) ";"
</code></pre>

<p>And here is what has fixed it..</p>

<pre><code>$url= str_replace("#038;", "", $url);
</code></pre>