How to get a json service in php - 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

Related

Cannot read the xml file using simplexml_load_file ();

I am having a problem reading data from an online xml file, when I use the simplexml_load_file () function; I encountered an error.
My code PHP:
$url = "https://www.bidv.com.vn/ServicesBIDV/ExchangeRateServlet";
$xml = simplexml_load_file($url);
echo '<pre>';
print_r($xml);
echo '</pre>';
input eror
Warning: simplexml_load_file(https://www.bidv.com.vn/ServicesBIDV/ExchangeRateServlet): failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden in D:\All In One\xammp\htdocs\ty_gia_api\index.php on line 12
Warning: simplexml_load_file(): I/O warning : failed to load external entity "https://www.bidv.com.vn/ServicesBIDV/ExchangeRateServlet" in D:\All In One\xammp\htdocs\ty_gia_api\index.php on line 12
Try to get contents from URL like:
$url = "https://www.bidv.com.vn/ServicesBIDV/ExchangeRateServlet";
$xml = simplexml_load_string(shell_exec("curl -k '$url'"));
echo '<pre>';
print_r($xml);
echo '</pre>';
I see the page you try to get is not XML, its JSON.
This will work:
$url = 'https://www.bidv.com.vn/ServicesBIDV/ExchangeRateServlet';
$data = json_decode(shell_exec("curl -k '$url'"), 1);
echo '<pre>';
print_r($data);
echo '</pre>';
var_dump($data);

How to check if API is able to handle the request using PHP

I have a PHP file calling a request to an API:
$url = https://api.spoonacular.com/recipes/search?query=spaghetti%bolognese&apiKey={{apikey}}3;
$response = json_decode(file_get_contents($url), true);
$name = $response["title"];
The $name should show what the json file's response's value is when the key is title.
I only have 150 requests allowed per day with my account so I am trying to only find $name when it can still make the request. No matter what I try I am still getting the following error:
file_get_contents(https://api.spoonacular.com/recipes/search?query=spaghetti%bolognese&apiKey={{apikey}}):
failed to open stream: HTTP request failed! HTTP/1.1 402
I have tried the following but don't know where I'm going wrong. Can anyone help?
$url = https://api.spoonacular.com/recipes/search?query=spaghetti%bolognese&apiKey={{apikey}}3;
if(file_get_contents($url), true) {
$name = $response["title"];
}

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.

HTTP request failed! HTTP/1.0 400 Bad Request when trying to get content from youtube

I get this error when I try to get content from youtube with more then one word.
Warning: file_get_contents(https://www.youtube.com/results?search_query=html begginer): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in C:\xampp\htdocs\sites\mime\beta\default.php on line 5
My code:
<?php
$address = $_GET['q'];
$address = 'https://www.youtube.com/results?search_query=' . $address;
$html = file_get_contents($address);
echo $html;
?>
However, if I try to get content from youtube using only one word (for example: http://127.0.0.1/sites/mime/beta/default.php?q=html), the script work perfectly.
What's wrong?
$address = $_GET['q']; // this GET can be spaced or not
This is an url so you must encode before put inside youtube query
$address = urlencode($_GET['q']);
PHP urlencode

File_get_content failed to open the stream

I have checked previous related threads, but my problem deals with specific readability API.
I am trying to get most relavant image from web page. I am using redhttps://www.readability.com/developers/api/parser for that.
Here is my code:
<?php
define('TOKEN', "1b830931777ac7c2ac954e9f0d67df437175e66e");
define('API_URL', "https://www.readability.com/api/content/v1/parser?url=%s&token=%s");
function get_image($url) {
// sanitize it so we don't break our api url
$encodedUrl = urlencode($url);
$TOKEN = '1b830931777ac7c2ac954e9f0d67df437175e66e';
$API_URL = 'https://www.readability.com/api/content/v1/parser?url=%s&token=%s';
// $API_URL = 'http://blog.readability.com/2011/02/step-up-be-heard-readability-ideas';
// build our url
$url = sprintf($API_URL, $encodedUrl, $TOKEN);
// call the api
$response = file_get_contents($url);
if( $response ) {
return false;
}
$json = json_decode($response);
if(!isset($json['lead_image_url'])) {
return false;
}
return $json['lead_image_url'];
}
echo get_image('https://www.facebook.com/');
?>
Error:
Warning: file_get_contents(https://www.readability.com/api/content/v1/parser?url=https%3A%2F%2Fwww.facebook.com%2F&token=1b830931777ac7c2ac954e9f0d67df437175e66e): failed to open stream: HTTP request failed! HTTP/1.1 403 FORBIDDEN in F:\wamp\www\inviteold\test2.php on line 16
You seem to be getting content from Facebook.
Due to your error 403 FORBIDDEN, I would say that your parser/code is not authorised to access the facebook page you are accessing and thus you are getting denied due to privacy settings.

Categories