title: how to pass data to JSON server using cURL with POST
body: <p>Update: the site I'm scraping has provided me with another way to get the data I need, so I am no longer having this problem. However I am still interested in a solution for educational reasons.</p>
  
  <hr>
  
  <p>I want to use cURL to pass data to a JSON server via POST.</p>
  
  <p>I'm not sure how to format the variables for the cURL.</p>
  
  <p>this doesn't work (returns null):</p>
  
  <pre><code>define('POSTVARS', 'json='.urlencode('{"cid":"2623","strQuery":"","strValues":"undefined","currentPage":"0","pageSize":"-1","pageSort":"-1","countryId":"2","maxResultCount":""}'));
  </code></pre>
  
  <p>nor does this</p>
  
  <pre><code>define('POSTVARS', "json=%7B'cid'%3A'2623'%2C%20'strQuery'%3A''%2C%20'strValues'%3A'undefined'%2C%20'currentPage'%3A'0'%2C%20'pageSize'%3A'-1'%2C'pageSort'%3A'-1'%2C'countryId'%3A'2'%2C'maxResultCount'%3A''%7D=");
  </code></pre>
  
  <p>As per <a href="https://stackoverflow.com/questions/3514131/post-json-using-curl">Post JSON using Curl</a> I am setting the header with</p>
  
  <pre><code>curl_setopt($ch, CURLOPT_HTTPHEADERS,array('Content-Type: application/json'));
  </code></pre>
  
  <p>My script works for regular POST variables, but not when I try to pass data to a JSON server, and I am wondering if I have misformatted the data or must provide some other additional parameter.</p>
  
  <p>full code of my attempt (formatting is kind of broken below, I pasted the full script here <a href="https://docs.google.com/document/d/1hokE6-oMtcs3MBgPUzPwJvZIWNABc3XjOO2IzbpxDF4/edit?hl=en&amp;authkey=CKXcnv8C" rel="nofollow noreferrer">https://docs.google.com/document/d/1hokE6-oMtcs3MBgPUzPwJvZIWNABc3XjOO2IzbpxDF4/edit?hl=en&amp;authkey=CKXcnv8C</a> ):</p>
  
  <p><pre><code></p>
  
  <p>function get_url( $url,  $javascript_loop = 0, $timeout = 5 )
  {
      $cookie = tempnam ("/tmp", "CURLCOOKIE");
      $ch = curl_init(POSTURL);
      curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
      curl_setopt( $ch, CURLOPT_URL, $url );
      curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookie );
      curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
      curl_setopt( $ch, CURLOPT_ENCODING, "" );
      curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
      curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
      curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );    # required for https urls
      curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
      curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );
      curl_setopt( $ch, CURLOPT_MAXREDIRS, 10 );</p>
  
  <pre><code>$postvars=&lt;&lt;&lt;HEREDOC
  {'cid':'2623', 'strQuery':"", 'strValues':'undefined', 'currentPage':'0', 'pageSize':'-1','pageSort':'-1','countryId':'2','maxResultCount':''}
  </code></pre>
  
  <p>HEREDOC;</p>
  
  <pre><code>curl_setopt($ch, CURLOPT_POST      ,1);
  curl_setopt($ch, CURLOPT_POSTFIELDS    ,$postvars);
  curl_setopt($ch, CURLOPT_HEADER      ,0);  // DO NOT RETURN HTTP HEADERS 
  
  $arr = array();
  array_push($arr, 'Content-Type: application/json; charset=utf-8');
  curl_setopt($ch, CURLOPT_HTTPHEADER, $arr);
  
  $content = curl_exec( $ch );
  $response = curl_getinfo( $ch );
  curl_close ( $ch );
  
  if ($response['http_code'] == 301 || $response['http_code'] == 302)
  {
      ini_set("user_agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1");
  
      if ( $headers = get_headers($response['url']) )
      {
          foreach( $headers as $value )
          {
              if ( substr( strtolower($value), 0, 9 ) == "location:" )
                  return get_url( trim( substr( $value, 9, strlen($value) ) ) );
          }
      }
  }
  
  if (    ( preg_match("/&gt;[[:space:]]+window\.location\.replace\('(.*)'\)/i", $content, $value) || preg_match("/&gt;[[:space:]]+window\.location\=\"(.*)\"/i", $content, $value) ) &amp;&amp;
          $javascript_loop &lt; 5
  )
  {
      return get_url( $value[1], $javascript_loop+1 );
  }
  else
  {
      return array( $content, $response );
  }
  </code></pre>
  
  <p>}</p>
  
  <p>// set url
  $url="http://us.asos.com/services/srvWebCategory.asmx/GetWebCategories";</p>
  
  <p>$output = get_url($url);</p>
  
  <p>print_r($output);</p>
  
  <p></pre></code></p>