2

I want get json from a url with file_get_contents but it replace & with &amp and it dosnt work this way i tried curl and happened again i try url directly too and it worked ,so url haven't problem

$url="https://api.kavenegar.com/v1/asdkljsadlkjsd/sms/send.json?receptor=09148523669&sender=100005252&message=testing";


$json = file_get_contents($url);
echo "1";
var_dump($json);

and this is the result:

( ! ) Warning: file_get_contents(https://api.kavenegar.com/v1/asdkljsadlkjsd/sms/send.json?receptor=09148523669&sender=100005252&message=testing): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in C:\wamp64\www\te\func\testjson.php on line 22

Of course stackoverflow remove &amp from result url.

rescobar
  • 1,261
  • 15
  • 25

2 Answers2

0

The trouble seems to be that it's returning an error status code. If so then see: Ignoring errors in file_get_contents HTTP wrapper?

$url="https://api.kavenegar.com/v1/asdkljsadlkjsd/sms/send.json?receptor=09148523669&sender=100005252&message=testing";

$context = stream_context_create(array('http' => array('ignore_errors' => true),));
$json = file_get_contents($url, false, $context);

echo $json;
9bO3av5fw5
  • 882
  • 5
  • 14
-2

this is the answer :

$url = 'https://example.url?';

// http_build_query builds the query from an array
$query_array = array (
    'search' => $string,
    'from' => $from,
    'to' => $to,
    'format' => 'json'
);

$query = http_build_query($query_array);
$result = file_get_contents($url . '&' . $query);