0

I've started making a webpage that uses an API from another website to get phone numbers for verification of websites, which I've gotten working with Python already. However, when I try to use cURL to get the JSON data from the API it returns nothing. The code for the PHP is below.

echo "Requesting number...";
$url = 'api.php'; # changed from the actual website

$params = array(
    'metod' => 'get_number', # misspelt because it is not an english website
    'apikey' => 'XXXXXXXXXXXXXXXXXXXXXXXXX',
    'country' => 'RU',
    'service' => 'XXXXX',
 );

$ch = curl_unit();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);

$result = curl_exec($ch);
if(curl_errno($ch) !== 0) {
    error_log('cURL error when connecting to ' . $url . ': ' . curl_error($ch));
}

curl_close($ch);

print_r($result);`

I expect that when the code is executed on the server it should give all of the JSON from the file, which I can then use later to pick out only certain parts of it to use elsewhere. However the actual results are that it does not print anything, as seen here: https://i.stack.imgur.com/6CzVU.jpg

Aidan
  • 13
  • 1
  • 1
  • Hi, Aidan, you can try https://www.getpostman.com/ for API debugging. Just install program to your pc, and you can try any type of API request. – JohnnyB1988 Dec 23 '18 at 14:11
  • Maybe check response http code `$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);` first – mcek Dec 23 '18 at 14:38
  • @JohnnyB1988 it isn't the API thats the problem, but rather I don't know how I can get the JSON in PHP, it works when I use my Python program (get JSON, read from it, etc) but in PHP it doesn't get the JSON. – Aidan Dec 23 '18 at 16:11
  • Have you tried using [file_get_contents](https://stackoverflow.com/a/2445332/1492977) instead? It also works with external URLs and POST method. – Anders Carstensen Dec 23 '18 at 17:06
  • @AndersCarstensen tried it again just now because it didn't work earlier, now everythings working. Thank you! – Aidan Dec 23 '18 at 18:34
  • @Aidan with Postman you will have and response and also source code, which you can put into file. There is and python source code for request, and for php and some other. just try it :D And if it gives you expected response, then you can decode it with json_decode($result) and work like with array or Object. – JohnnyB1988 Dec 23 '18 at 18:36

1 Answers1

-1

I'm not sure why your code doesn't work, but a simpler alternative could be to use file_get_contents:

echo "Requesting number...";
$url = 'api.php'; # changed from the actual website

$postdata = http_build_query(
    array(
        'metod' => 'get_number', # misspelled because it is not an English website
        'apikey' => 'XXXXXXXXXXXXXXXXXXXXXXXXX',
        'country' => 'RU',
        'service' => 'XXXXX'
    )
);

$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $postdata
    )
);

$context = stream_context_create($opts);
$result = file_get_contents($url, false, $context);
print_r($result);
Anders Carstensen
  • 2,949
  • 23
  • 23