title: php - CURL - Getting HTTP code if/else to test
body: <p>I want to do 404 testing on an API using curl and php</p>
  
  <p>While valid requests to the token generation endpoint (/api/Utilities/StashData) will receive an HTTP 200 response containing a token for use with Leap, invalid requests will receive an HTTP 400 response (Bad Request). If your system receives an HTTP 400 response from the API, it must not attempt to proceed to Leap. The content of this response will contain a JSON-serialized object describing the validation error(s), as per the following example:</p>
  
  <pre><code>[
  
         {
  
                "Key": "CapeConsumers.TrackerNumber",
  
                "Errors": [
  
                       {
  
                             "ErrorCode": "CC002",
  
                             "Description": "The value is required."
  
                       }
  
                ]
  
         }
  
  ]
  </code></pre>
  
  <p>As you can see, the response contains an array of objects, where each object has a Key property which refers to the key submitted to the service, and an Errors property describing the reason(s) the value was rejected. The ErrorCode values are predefined and guaranteed never to change, the Description is dynamic for certain errors and exists mainly to aid debugging</p>
  
  <p>The codes </p>
  
  <blockquote>
    <p>CC001     </p>
    
    <p>Invalid value.    </p>
    
    <p>The value does not conform to one or more validation rules defined for
    the key.</p>
    
    <p>CC002     </p>
    
    <p>The value is required.    </p>
    
    <p>A required value has been omitted from the request. Sending null or a
    blank string for a required value will also result in this error
    (unless a blank string has been defined as valid by supporting
    documentation).</p>
    
    <p>CC003     </p>
    
    <p>The value is not of the correct type. Expected {0}, found {1}.    </p>
    
    <p>The system expected the value to be of one type, but a different type
    was submitted. (e.g. a number was sent instead of a string)</p>
    
    <p>CC004     </p>
    
    <p>The origin is not valid for the specified campaign.   </p>
    
    <p>Campaigns are linked to specific integrating parties. If an
    integrating party uses a tracker number that isn’t assigned to it,
    this error will be returned.</p>
    
    <p>CC005     </p>
    
    <p>The campaign is not currently active.     </p>
    
    <p>The tracker number references a campaign that either hasn’t started or
    has already come to an end.</p>
  </blockquote>
  
  <p>Here is the code I have tried.</p>
  
  <pre><code>$endpointUser = "myuser";
  //
  $endpointPassword = "mypass";
  //
  $url = "https://webservices_test.capeconsumers.co.za/api/Utilities/StashData";
  
  $iframeUrl =  "https://onlineapplicationtest.capeconsumers.co.za/Bridge/CapeConsumersSACommercial?token=";
  
  $fields = array(
      "User.IdentityNumber"=&gt; $_GET['security_phrase'],
      "CapeConsumers.TrackerNumber"=&gt; "6E273247DB4840G3",
      "Call.AgentReference"=&gt; $_GET['user'] ,
      "Call.RecordingReference"=&gt; $_GET['security_phrase']
  );
  
  $process = curl_init($url);
  curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  curl_setopt($process, CURLOPT_HEADER, 0);
  curl_setopt($process, CURLOPT_USERPWD, $endpointUser . ":" . $endpointPassword);
  curl_setopt($process, CURLOPT_TIMEOUT, 30);
  curl_setopt($process, CURLOPT_POST, 1);
  curl_setopt($process, CURLOPT_POSTFIELDS, json_encode($fields));
  curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
  $token = curl_exec($process);
  curl_close($process);
  $token = str_replace('"','',$token);
  
  $httpCode = curl_getinfo($process, CURLINFO_HTTP_CODE);
  
  
  if($httpCode == 400) {
  
   echo "there was an error"; exit;
  
  }
  
  ?&gt;
  
  
  &lt;iframe src="&lt;?php echo $iframeUrl . $token; ?&gt;" width="100%" height="800"&gt;&lt;/iframe&gt;
  </code></pre>
  
  <p>My problem is that if I deliberately give it a bad response, it will still generate the iframe instead of exiting at the if statement for the 400 error. </p>
  
  <p>I am unsure of my logic as this is my first attempt at curl </p>