Strange behavior of cURL request in PHP script




I am experiencing strange behavior of a cURL request that I am making in my PHP code. I am running the code locally on a standard WAMP Apache server. Here's the code:



```
$auth_info = "...";
$some_url = "...";
$channel = curl_init();
curl_setopt($channel, CURLOPT_URL, $some_url);
curl_setopt($channel, CURLOPT_HTTPHEADER, 
    array("Authorization: BASIC " . base64_encode($auth_info))
);
curl_setopt($channel, CURLOPT_RETURNTRANSFER, true);
curl_setopt($channel, CURLOPT_HEADER, true);
$response = curl_exec($channel);
var_dump(curl_getinfo($channel));
$header_size = curl_getinfo($channel, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
var_dump($header);
var_dump($body);
curl_close($channel);

        

        
        
        
 (adsbygoogle = window.adsbygoogle || []).push({});
 
        

    
```

If I execute this little PHP code snippet through my **CLI** (Powershell since I am on Windows) **everything is fine** , all var\_dumps are working and I can see the $ header and $ body and everything and the data I expect is really are present.


Now for the weird behavior. If I open the script file with the above snippet in **any browser** it just gives me:



```
array (size=26)
'url' => string 'the\_url\_here' (length=39)
'content\_type' => null
'http\_code' => int 0
'header\_size' => int 0
'request\_size' => int 0
'filetime' => int -1
'ssl\_verify\_result' => int 0
'redirect\_count' => int 0
'total\_time' => float 1.234
'namelookup\_time' => float 0
'connect\_time' => float 0.109
'pretransfer\_time' => float 0
'size\_upload' => float 0
'size\_download' => float 0
'speed\_download' => float 0
'speed\_upload' => float 0
'download\_content\_length' => float -1
'upload\_content\_length' => float -1
'starttransfer\_time' => float 0
'redirect\_time' => float 0
'redirect\_url' => string '' (length=0)
'primary\_ip' => string 'here\_is\_the\_ip' (length=12)
'certinfo' => 
  array (size=0)
    empty
'primary\_port' => int 443
'local\_ip' => string 'here\_is\_my\_ip' (length=13)
'local\_port' => int -> my_local_port_here
boolean false
boolean false

        

        
        
        
 (adsbygoogle = window.adsbygoogle || []).push({});
 
        

    
```

I am completely puzzled as I cannot see the difference between script beeing running CLI and running browser. Anyone got an idea about this?


EDIT Note. If I use Guzzle for the request, everything works fine, both in the CLI and in the browser. I'm still interested in the answer why cURL is causing problems here.
Have you tried verbose output for Curl's query?


I usually think this is the best way to figure out what's going on under the hood ... [fooobar.com/questions/42395 / ...](https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=)



  




 (adsbygoogle = window.adsbygoogle || []).push({});
 
  

Also not that popular, but this approach looks simple to implement and is much cleaner ... [fooobar.com/questions/42395 / ...](https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=)
Hi BenzZz,



as far as I understand this is a HTML-problem. Try setting the initial value with



```
value=""
```

to an empty string.



Otherwise I can suggest:  

- Open your source code in your browser and see if you have any artifacts from your php parser  

- make sure you have your JavaScript deactivated



I hope that helps, Simon Errors and warnings usually appear in `....\logs\php_error.log` or `....\logs\apache_error.log` depending on your php.ini settings.


Also useful errors are often directed to the browser, but as they are not valid html they are not displayed.


So `"tail -f`" your log files and when you get a blank screen use IEs "view" -> "source" menu options to view the raw output. JavaScript *itself* has nothing built in for handling query string parameters. 


Code running in a (modern) browser you can use the [`URL` object](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams) (which is part of the APIs provided by browsers to JS):





```
var url_string = "http://www.example.com/t.html?a=1&b=3&c=m2-m3-m4-m5"; //window.location.href
var url = new URL(url_string);
var c = url.searchParams.get("c");
console.log(c);
```









For older browsers (including Internet Explorer), you can use [this polyfill](https://github.com/ungap/url-search-params) or the code from the original version of this answer that predates `URL`:


You could access `location.search`, which would give you from the `?` character on to the end of the URL or the start of the fragment identifier (#foo), whichever comes first.


Then you can parse it with this:





```
function parse_query_string(query) {
  var vars = query.split("&");
  var query_string = {};
  for (var i = 0; i < vars.length; i++) {
    var pair = vars[i].split("=");
    var key = decodeURIComponent(pair[0]);
    var value = decodeURIComponent(pair[1]);
    // If first entry with this name
    if (typeof query_string[key]  "undefined") {
      query_string[key] = decodeURIComponent(value);
      // If second entry with this name
    } else if (typeof query_string[key]  "string") {
      var arr = [query_string[key], decodeURIComponent(value)];
      query_string[key] = arr;
      // If third or later entry with this name
    } else {
      query_string[key].push(decodeURIComponent(value));
    }
  }
  return query_string;
}

var query_string = "a=1&b=3&c=m2-m3-m4-m5";
var parsed_qs = parse_query_string(query_string);
console.log(parsed_qs.c);
```




You can get the query string from the URL of the current page with:



```
var query = window.location.search.substring(1);
var qs = parse_query_string(query);

``` You have some minor problems to remove. This first batch is useless paid-search listings to make money for **HP**:



R1 - HKCU\Software\Microsoft\Internet Explorer\Main,Search Bar = [http://srch-qus9.hpwis.com/](/posts/bad_link/25407?url=http%3A%2F%2Fsrch-qus9.hpwis.com%2F)  

R1 - HKCU\Software\Microsoft\Internet Explorer\Main,Search Page = [http://srch-qus9.hpwis.com/](/posts/bad_link/25407?url=http%3A%2F%2Fsrch-qus9.hpwis.com%2F)  

R1 - HKCU\Software\Microsoft\Internet Explorer\Main,Default\_Page\_URL = [http://qus9.hpwis.com/](/posts/bad_link/25407?url=http%3A%2F%2Fqus9.hpwis.com%2F)  

R1 - HKCU\Software\Microsoft\Internet Explorer\Main,Default\_Search\_URL = [http://srch-qus9.hpwis.com/](/posts/bad_link/25407?url=http%3A%2F%2Fsrch-qus9.hpwis.com%2F)  

R0 - HKLM\Software\Microsoft\Internet Explorer\Main,Start Page = [http://qus9.hpwis.com/](/posts/bad_link/25407?url=http%3A%2F%2Fqus9.hpwis.com%2F)  

R1 - HKLM\Software\Microsoft\Internet Explorer\Main,Search Bar = [http://srch-qus9.hpwis.com/](/posts/bad_link/25407?url=http%3A%2F%2Fsrch-qus9.hpwis.com%2F)  

R1 - HKLM\Software\Microsoft\Internet Explorer\Main,Search Page = [http://srch-qus9.hpwis.com/](/posts/bad_link/25407?url=http%3A%2F%2Fsrch-qus9.hpwis.com%2F)  

R1 - HKLM\Software\Microsoft\Internet Explorer\Main,Default\_Page\_URL = [http://qus9.hpwis.com/](/posts/bad_link/25407?url=http%3A%2F%2Fqus9.hpwis.com%2F)  

R1 - HKLM\Software\Microsoft\Internet Explorer\Main,Default\_Search\_URL = [http://srch-qus9.hpwis.com/](/posts/bad_link/25407?url=http%3A%2F%2Fsrch-qus9.hpwis.com%2F)



This is a problem BHO:



O2 - BHO: (no name) - {FDD3B846-8D59-4ffb-8758-209B6AD74ACC} - (no file)



That's all I see for now. Are you running **DSL**? this is because the /login page, once a login occurs, the ‘state’ parameter is missing and so when the page tries to call the /authorize url it will error. the state parameter is used to mitigate csrf attack and so during authentication, the application sends this parameter in the authorization … i just solved the problem myself. there was something wrong with my browser. after a restart everything is working as expected. sorry for the hassle. Have you tried restarting your browser? you could use on-demand tls with local certs if you don’t know ahead of time which ip the request will come from:

tls internal {

on\_demand

}

[image]

[tls (caddyfile directive) - caddy documentation](https://caddyserver.com/docs/caddyfile/directives/tls)

caddy is a powerful, enterprise-ready, open source web server with automatic https writ… we’re glad to announce that this feature is now available in production on [bitrise.io](http://bitrise.io) :tada:

related [#changelog](/c/6-changelog) / [#announcements](http://discuss.bitrise.io/tags/announcements) :

[image]

[secret env vars and (generic) file storage files: control whether it’s available in pull request builds](http://discuss.bitrise.io/t/secret-env-vars-and-generic-file-storage-files-control-whether-it-s-available-in-pull-request-builds/2790) [changelog](/c/changelog)

related [#feature-r…](/c/9-feature-request) pull request sent and accepted. this bug is resolved.