title: How to provide a value to API and get the response using PHP?
body: <p>I am trying to receive data from an API based on the data I will send to it using PHP.</p>
  <p>Forexample: I connect to API and send it bookId = 3, the API sends a response isPurchased = true.</p>
  <p><strong>API:</strong></p>
  <p>This is what I want to achieve using PHP. This is what I do in Postman.</p>
  <ol>
  <li><p>Connect to the API's URL.</p>
  <p><a href="https://myapi.com/books" rel="nofollow noreferrer">https://myapi.com/books</a></p>
  </li>
  <li><p>Using POST, send a request by Entering bookId.</p>
  <p>{bookId : 2}</p>
  </li>
  <li><p>This is the response I get from the API.</p>
  <p>{</p>
  <p>bookId : 2,</p>
  <p>isPurchased : true,</p>
  <p>bookTitle : 'War and Peace'</p>
  <p>}</p>
  </li>
  </ol>
  <p>I want to achieve the exact same process in PHP.</p>
  <p>I have the following code so far. I am not sure how I can achieve my purpose.</p>
  <p>Any help is greatly appreciated.</p>
  <p><strong>PHP:</strong></p>
  <pre><code>&lt;?php
  
  $isPurchased = false;
  $bookId = 2;
  
  // Call API:
  $ch = curl_init();
  $url = &quot;https://myapi.com/books&quot;;
  
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_POSTFIELDS,&quot;bookId=&quot;.$bookId);
  //bookId is from API and $bookId is from user. - I am not sure if this is how you send the
  //info to the API but this is how I have seen it done in a tutorial.
  
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $json = curl_exec($ch);
  $obj = json_decode($json);
  
  //print_r($obj);
  if ($obj-&gt;{'isPurchased'} == true)
  {
    $isPurchased = TRUE;
  }
  
  curl_close ($ch);
  
  ?&gt;
  </code></pre>
  <p>When I run this code, I don't receive anything from the API.
  Am I doing something wrong?</p>
  <p>Again, any help with this would be much appreciated.</p>
  <p>If there is any other, easier method, I am more than willing to give it a go.</p>