0


I'm having simple script which comunicate via REST with some server. Server architecture have some holes, and I fight with one of them right now. When I send one request to assign some profile and don't wait for response and send second request. It basicly just scrap the first request and do the second one only.

My code looks like:

$this->assignProfile($token, $id, FIRST);   
$this->assignProfile($token, $id, SECOND);  

My Assign profile method looks like:

public function assignProfile($token, $organizationId, $profileId)
{
    //define enviroment and path
    $host = enviroment;     
    $path = "/admin/members/".$organizationId."/assigned-profiles";

    $data_string = '["'.$profileId.'"]';    

    // set up the curl resource
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $host.$path);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);        
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
        'Content-Type: application/json',                                                                                
        'Authorization: Bearer '.$token.'',      
        'Content-Length: ' . strlen($data_string)                                                                       
    ));

    echo "<br>OK<br>";
    // execute the request      
    $output = curl_exec($ch);       
    curl_close($ch);    
} 

The thing is, webservice is not response with any body or something, it just response with code 200. How can I code this to wait for a 200 and than continue with second request.

1 Answer 1

1

In your assignProfile function you can check the status code and return a value. According to that value you can decide to call the function for a second time.

  curl_exec($curl);
  $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  curl_close($curl);

  if (200 == $statusCode)
        return true;
  else
        return false;
Sign up to request clarification or add additional context in comments.

1 Comment

Ty, didn't know about this CURL function. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.