title: PHP curl POST request with JSON response
body: <p>I'm trying to send post data and receive an SID from an api(qbittorrent) to send with a get request later. I'm currently stuck with an OK response but the json is NULL. Here's my code:</p>
  
  <pre><code>&lt;?php
    $ch = curl_init();
    $fields = array( 'username'=&gt;'Admin','password'=&gt;'mypassword');
    $postvars = '';
    foreach($fields as $key=&gt;$value) {
      $postvars .= $key . "=" . $value . "&amp;";
    }
    $postvars = rtrim($postvars, '&amp;');
    $url = "https://192.123.123.123:8080/login";
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_POST, 1);         
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, 0);
    $headers= array('Accept: application/json','application/x-www-form-urlencoded'); 
    curl_setopt($ch,CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch,CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$postvars);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT ,3);
    curl_setopt($ch,CURLOPT_TIMEOUT, 20);
    $response = curl_exec($ch);
    var_dump(json_decode($response, true));
    print "curl response is:" . $response;
    curl_close ($ch);
  ?&gt;
  </code></pre>
  
  <p>You can see the API documentaiton here <a href="https://github.com/qbittorrent/qBittorrent/wiki/WebUI-API-Documentation#authorization" rel="nofollow">https://github.com/qbittorrent/qBittorrent/wiki/WebUI-API-Documentation#authorization</a></p>
  
  <p>I was able to get the json using powershell as simply as :</p>
  
  <pre><code>$prms = @{username="Admin";password="mypassword"}
  $res = Invoke-WebRequest https://192.123.123.123:8080/login -Method POST -Body prms -UseBasicParsing
  </code></pre>
  
  <p>but I'm unable to do so with php! If you also feel like heading me right for the get request using the SID as a cookie, i'll be more than welcome :)</p>