file_get_contents giving warning - php

$url="http://www.smszone.in/sendsms.asp?page=".$page."&username=".$usrnm."&password=".$passw."&number=".$mobile."&message="."Hi ".$admi.' '.implode($msg)." Is issued ..."."\n"."Regards";
$homepage = file_get_contents($url);
if($homepage)
{
echo "Message Send Compleated...";
}
else{
echo "Something Went Wrong...";
Warning: file_get_contents(http://www.smszone.in/sendsms.asp?page=SendSmsBulk&username=*******&password=****&number=**********&message=Hi Izza AMGR-8A:3/1-BCS-GR:2/1-CCL Is issued ... Regards): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in F:\xampp\htdocs\loglib\logissue.php on line 40
Something Went Wrong...
This is the error i am getting when i run this through my php code. If i copy the generated url and paste in any browser, it works....
Note: I am using xampp

You can use http_build_query function to build correct query string:
$query = [
'page' => $page,
'username' => $user,
// ...
];
$url = 'http://...?'.http_build_query($query);

Related

I am trying to get user country of location from geoplugin.net but i am getting an error

The Error I get is:
Warning:
file_get_contents(http://maps.google.com/maps/api/geocode/json?address=&sensor=false)
[function.file-get-contents]: failed to open stream: HTTP request
failed! HTTP/1.0 400 Bad Request in
D:\xampp\htdocs\SwaziTour\index.php on line 57
my code is as below:
echo var_export( unserialize( file_get_contents( 'http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR'] ) ) );
Don't use geoplugin.net. Try this:
$ip = $_SERVER['REMOTE_ADDR'];
$details = json_decode(file_get_contents("http://ipinfo.io/{$ip}"));
print_r($details);
Did you even try to open your link in browser? If you open your url you'll see that you get an error because you don't have an address.

PHP file_get_contents() throws error "failed to open stream: HTTP request failed! <!doctype html>"

I'm trying to return JSON results from a page but I get the following error using file_get_contents(): " failed to open stream: HTTP request failed! "
Can someone tell me why I get this error?
<?
$newURL = 'https://api.qwant.com/api/search/images?count=10&offset=1&q=Spicy
Cranberry Cavolo Nero (Kale)';
$returnedData = file_get_contents($newURL);
?>
Never use <? ?>. Use only <?php ?> or <?= ?>
urlencode() use only on values of your parameters, not on the whole parameter's line.
file_get_contents() is not a really good method to receive data from the outer servers. Better use CURL.
<?php
// Your URL with encoded parts
$newURL = 'https://api.qwant.com/api/search/images?count=10&offset=1&q='.urlencode('Spicy Cranberry Cavolo Nero (Kale)');
// This is related to the specifics of this api server,
// it requires you to provide useragent header
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERAGENT => 'any-user-agent-you-want',
);
// Preparing CURL
$curl_handle = curl_init($newURL);
// Setting array of options
curl_setopt_array( $curl_handle, $options );
// Getting the content
$content = curl_exec($curl_handle);
// Closing conenction
curl_close($curl_handle);
// From this point you have $content with your JSON data received from API server
?>

Php file_get_contents Special Characters

im gonna use an api to get player details. Some names are with special characters.
Name Bausí
Url: https/eu.api.battle.net/wow/character/Blackrock/Bausí?fields=statistics&locale=en_GB&apikey=xxx
if i use file_get_contens() there is no response.
Names without special characters works perfectly. I already used rawurlencode() and urlencode(). Both are not working. What can i do?
I read something about urlencode() and rawurlencode() is server dependant.
Thanks Chzn
Well i dont know whats wrong.
$charname = mb_convert_encoding("Bausí", "HTML-ENTITIES", "UTF-8");
$realm = "Blackrock";
$_SESSION["region"] = "eu";
$opts = array('https' => array('header' => 'Accept-Charset: UTF-8, *;q=0'));
$context = stream_context_create($opts);
$char_params = array(
'fields' => "statistics",
'locale' => "en_GB",
'apikey' => "my_api_key"
);
$char_url = "https://".$_SESSION['region'].".api.battle.net/wow/character/".str_replace("'","",$realm)."/".$charname."?".http_build_query($char_params);
if (!$json_char_o = file_get_contents($char_url, false, $context)) {
$error = error_get_last();
echo "HTTPs request failed. Error was: " . $error['message'];
}else{
echo $json_char_decoded = json_decode($json_char_o);
}
it throws me an error:
HTTP request failed. Error was: file_get_contents(https://eu.api.battle.net/wow/character/Blackrock/Bausí?fields=statistics&locale=en_GB&apikey=my_api_key): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
The response should be a json object.
If i open the link manually it works perfectly. What is wrong? :/

How to get a json service in php

This is the following code i am using
<?php
$contents= file_get_contents('http://skillpage.enigmateleco.net/rest/login.php?email=sri.sapna14#gmail.com&pass=12345');
$contents = utf8_encode($contents);
$contents_array = json_decode($contents,1);
?>
code is showing error
Warning: file_get_contents(http://...#gmail.com&pass=12345): failed to
open stream: HTTP request failed! HTTP/1.1 403 ModSecurity Action in
C:\xampp\htdocs\skillbook\json1.php on line 9
try to use this : $data = json_decode($request,true); this work only if your request is on JSON format

failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request

It seems out of no where my server is acting up. and I'm getting this error.
failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request
This is my code that I've had implemented for over a year now, and is confirmed working on a separate staging server.
$url = 'http://graph.facebook.com/10150624051911279/photos/all';
$response = json_decode(file_get_contents($url, true));
foreach ($response->data as $photo) {
echo '<li><a href="' . $photo->link . '" ><img class="img" src="timthumb.php?src=' . $photo->source . '&h=175&w=175" /></a></li>';
}
What could possibly be failing on my server to cause this issue. I'm stumped.
The request should fail, because to access that URL you need to obtain an access token. So Facebook returns
{
"error": {
"message": "An access token is required to request this resource.",
"type": "OAuthException",
"code": 104
}
}
with along with a "400 Bad Request" status. Additionally since you're opening an URL the second parameter of file_get_contents should be false (there is no point to search the include path, if you know it's not there).
To still get the response from Facebook and ignore the error you can do:
$url = 'http://graph.facebook.com/10150624051911279/photos/all';
$context = stream_context_create(array(
'http' => array(
'ignore_errors'=>true,
'method'=>'GET'
// for more options check http://www.php.net/manual/en/context.http.php
)
));
$response = json_decode(file_get_contents($url, false, $context));

Categories