Search code examples
phpcurlmashape

How to Curl from Mashape in PHP


I have got the following curl code to check whether a number is DND activated or not. But i don't know how to use this. How can i implement this in php to get json output.

CURL code:

curl --get --include "https://dndcheck.p.mashape.com/index.php?mobilenos=9999999999%2C8888888888" \
  -H "X-Mashape-Key: g5Svg3wHuomshHIyjncC0hetIUVXp1h7E0LjsnJmorZlVxUcQV"

Link to api and documentation: https://www.mashape.com/blaazetech/dnd-check


Solution

  • Try this and tell me how it is working for you:

    <?php
    // Create a stream
    $opts = array(
      'http'=>array(
        'method'=>"GET",
        'header'=>"X-Mashape-Key: g5Svg3wHuomshHIyjncC0hetIUVXp1h7E0LjsnJmorZlVxUcQV"               
      )
    );
    
    $context = stream_context_create($opts);
    
    // Open the file using the HTTP headers set above
    $res = file_get_contents('https://dndcheck.p.mashape.com/index.php?mobilenos=9999999999%2C8888888888', false, $context);
    print_r(json_decode($res, true));
    ?>
    

    Hope this is enough to get you started.
    In practice I guess you will have to make the key and the phone number obtained by a variables, but this is trivial.