title: cURL Equivalent to xhrFields: {withCredentials: true}
body: <h1>Goal:</h1>
  
  <p>Log into processor's site using their API and store the success response into COOKIEs and SESSIONs on my site.</p>
  
  <p><strong>What almost works:</strong></p>
  
  <pre><code>jQuery(document).ready(function($){
      $.ajax({
        type: 'POST',
        url: '{url-to-api-call}',
        xhrFields: {
          withCredentials: true
        },
        dataType: 'text',
        data: 'Email={email}&amp;GuestSessionToken={token}&amp;Password={pass}&amp;Format=JSON&amp;RememberMe=true',
        processData: false,
        crossDomain: true,
        success: function (res) { console.log('success'); },
        error: function (jqXHR, textStatus, ex) {
            console.log('error');
        }
      });
  });
  </code></pre>
  
  <p><strong>Why it doesn't work:</strong>
  I'm unable to store the response to COOKIEs and SESSIONs.</p>
  
  <hr>
  
  <p><strong>What I would like to work:</strong></p>
  
  <pre><code>$url = {url-to-api};
  $curl = curl_init();
  $curl_post_data = array(
      'Email' =&gt; $fields['user_email'],
      'GuestSessionToken' =&gt; $_COOKIE['SessionToken'],
      'Password' =&gt; $fields['user_pass'],
      'Format' =&gt; "JSON",
      'RememberMe' =&gt; "true"
  );
  curl_setopt($curl,CURLOPT_URL,$url);
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
  curl_setopt($curl, CURLOPT_POST, true);
  curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
  curl_setopt($curl,CURLOPT_CONNECTTIMEOUT,5);
  curl_setopt($curl, CURLOPT_TIMEOUT, 90);
  
  $content = curl_exec($curl);
  curl_close($curl);
  $content_array = json_decode($content, true);
  
  if ($content_array['StatusCode'] == 'OK') {
      $fields['UserId'] = $content_array['Data']['UserId'];
      $fields['SessionID'] = $content_array['Data']['ID'];
      $fields['SessionToken'] = $content_array['Data']['Token'];
      return true;
  } else {
      $errors-&gt;add( 'error', 'Unable to sign in.' );
      return false;
  }
  </code></pre>
  
  <p><strong>Why it doesn't work:</strong>
  This will return a success response that I can use to set the COOKIEs and SESSIONs, but it will not log the user into the processor's site.</p>
  
  <p><strong>Why I think it doesn't work:</strong>
  The ajax call wouldn't sign the user in until I added the xhrFields part. I think if I could find a cURL equivalent for the xhrFields part, I would be all set.</p>
  
  <p><strong>Thanks in advance!</strong></p>
  
  <h2>My duct tape fix.</h2>
  
  <pre><code>&lt;?php
  
  // If signin form is submitted and signin cURL was successful.
  echo (isset($fields['signin_script'])) ? $fields['signin_script'] : "";
  
  function processor_signin(&amp;$fields, &amp;$errors) {
  
      $url = '{url-to-api-call}';
      $curl = curl_init();
      $curl_post_data = array(
          'Email' =&gt; $fields['user_email'],
          'GuestSessionToken' =&gt; $_COOKIE['SessionToken'],
          'Password' =&gt; $fields['user_pass'],
          'Format' =&gt; "JSON",
          'RememberMe' =&gt; "true"
      );
      curl_setopt($curl,CURLOPT_URL,$url);
      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
      curl_setopt($curl, CURLOPT_POST, true);
      curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
      curl_setopt($curl,CURLOPT_CONNECTTIMEOUT,5);
      curl_setopt($curl, CURLOPT_TIMEOUT, 90);
  
      $content = curl_exec($curl);
      curl_close($curl);
      $content_array = json_decode($content, true);
  
      if ($content_array['StatusCode'] == 'OK') {
          $fields['UserId'] = $content_array['Data']['UserId'];
          $fields['SessionID'] = $content_array['Data']['ID'];
          $fields['SessionID'] = $content_array['Data']['ID'];
          $fields['SessionToken'] = $content_array['Data']['Token'];
  
          // Duct tape fix
          $fields['signin_script'] = "
          &lt;script&gt;
              jQuery(document).ready(function($){
                  $.ajax({
                    type: 'POST',
                    url: '{url-to-api-call}',
                    xhrFields: {
                      withCredentials: true
                    },
                    dataType: 'text',
                    data: '
                      Email=".$fields['user_email']."
                      &amp;GuestSessionToken=".$_COOKIE['SessionToken']."
                      &amp;Password=".$fields['user_pass']."
                      &amp;Format=JSON
                      &amp;RememberMe=true',
                    processData: false,
                    crossDomain: true,
                    success: function (res) { console.log('signin success'); },
                    error: function (jqXHR, textStatus, ex) {
                        console.log('signin error');
                    }
                  });
              });
          &lt;/script&gt;
          ";
  
          return true;
      } else {
          $errors-&gt;add( 'error', 'Unable to sign in.' );
          return false;
      }
  
  }
  ?&gt;
  </code></pre>