Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to set CURLINFO_HTTP_CODE
    primarykey
    data
    text
    <p>I am building an API to store registered users data. Data is passed using <code>CURL with an api-key</code>. I need to check the api-key is valid or not. If it is not valid return an <strong>error message with an CURLINFO_HTTP_CODE</strong>. I returned the message. <strong>But the CURLINFO_HTTP_CODE is 200. I need to change it to 500</strong>. How to do that? I am using <strong>SLIM framework</strong></p> <p>Data Posting code.</p> <pre><code>private static function postJSON($url, $data) { $jsonData = array ( 'json' =&gt; json_encode($data) ); // data array contains some data and api-key $jsonString = http_build_query($jsonData); $http = curl_init($url); curl_setopt($http, CURLOPT_HEADER, false); curl_setopt($http, CURLOPT_RETURNTRANSFER, true); curl_setopt($http, CURLOPT_POST, true); curl_setopt($http, CURLOPT_HTTPHEADER, array('Content-type: application/json')); curl_setopt($http, CURLOPT_HTTPHEADER, array( 'Content-Type: application/x-www-form-urlencoded', 'Content-Length: '.strlen($jsonString) )); curl_setopt($http, CURLOPT_POSTFIELDS, $jsonString); $responseBody = curl_exec($http); $statusCode = curl_getinfo($http, CURLINFO_HTTP_CODE); echo $statusCode; // need to get 500 if the api key is not registerd if($statusCode &gt; 200) { error_log('BugTracker Warning: Couldn\'t notify ('.$responseBody.')'); } curl_close($http); return $statusCode; } </code></pre> <p>Following is the code which will respond to the CURL operation</p> <pre><code>$apiKey = $event-&gt;apiKey; // Check if the project for the given api key exists. if not do not insert the bugs $projectDetails = $bt-&gt;getProjectDetails($apiKey); if(count($projectDetails)&gt;0){ print_r($projectDetails); foreach($event-&gt;exceptions as $bug){ $errorClass = $bug-&gt;errorClass; $message = $bug-&gt;message; $lineNo = $bug-&gt;lineNo; $file = $bug-&gt;File; $createdAt = $bug-&gt;CreatedAt; //saving to db $bt-&gt;saveData($releaseStage,$context,$errorClass,$message,$lineNo,$file,$createdAt,$apiKey); } } else{ // show error with 500 error code http_response_code(500); echo "Invalid project"; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload