Ajax from within PHP - php

I know AJAX will not be what it is called, but I am looking for something similar that can be done from within PHP itself (not using javascript).
Basically, as the PHP is creating the page, I want to query an API to gather some information that will be used on the current page. Is this possible, and if so what would be the best method?
Thanks

You can use fopen or curl:
Here is an example to open a connection to twitters API, read the public timeline and output parts of it.
<?php
$fp = fopen("http://api.twitter.com/1/statuses/public_timeline.json?count=3&include_entities=false","r");
while($data = fgets($fp))
{
$json .= $data;
}
$arr = json_decode($json);
print_r($arr);
?>

You are probably wanting curl.
http://php.net/manual/en/book.curl.php

If you're talking about a remote HTTP API, you would utilize cURL at runtime. It's basically PHP's way of accessing information across domains.
The above can be a little overwhelming - check out the simple example page to get you started. If available, I suggest working with cURL from the command line, as there's quite a few options on there. It's a good way to get familiar with the command and different options available.

Related

PHP - how to find the meta information of an internal/built-in method programmatically?

Is it possible to find the data about a built in method in PHP via code?
For example, we have array_key_exists() which is an internal method.
I want to find out the parameters in this function programatically. The reason is, there is an up coming interview, and I will have to write code on Notepad. There will not be any internet connection to see PHP documentation.
If I can get the information about built in methods via code, it will be really helpful.
Is it at all possible to print meta data of a function? I am not asking about user defined functions, but about PHP's built in functions.
Thanks a lot.
You are looking for Reflection i think:
$refFunc = new ReflectionFunction('preg_replace');
foreach( $refFunc->getParameters() as $param ){
print $param;
}
http://php.net/manual/de/class.reflectionfunction.php

How to make Https Requests in PHP

I`m still not really comfortable with PHP and furthermore completly new to Json. What i want to do is check out if a specific Twitch.tv Livestream is online or not, also i want to get the viewers.
Twitch.tv has its own API ( https://github.com/justintv/Twitch-API/wiki/API ) with which, this should be possible. Here is my first approach:
// Twitch Streams
else if($typ == 't') {
$api = file_get_contents("https://api.twitch.tv/kraken/streams/".$stream_id);
$json = json_decode($api);
if($json->stream != null) {
return true;
}
else {
return false;
}
}
The problem is that file_get_contents seem to not understand https, it claimes that the wrapper is not installed. The thing is that normal http requests are not supported by the API, so i guess i have to use another function. Can somebody suggest me a different (not so hard to use) function. I have read about curl and fsocket but they seem a bit difficult to use for me.
https:// might not be enabled. This post might help you find out why: How to get file_get_contents() to work with HTTPS?. There should be no reason why you have to use cURL, but cURL is usually more flexible so i would advice you to use that instead if you are able to comprehend how it is working.
PHP Curl should be able to help with this.
The link below illustrates how to get the http code:
http://php.net/manual/en/function.curl-getinfo.php

Get Message in PHP

I'm trying very simple in PHP and not very sure what to search here or on google.
Problem is -
In PHP function I want to call/get a URL
http://www.example.com/message?Name=MyNameIsX
and like to read the return value (body) at this URL (which may contain "Your Name is MyNameIsX")
I tried
$data = file_get_contents($url)
This is timing out; although I'm able to open the $url in the browser.
Yes, file_get_contents normal use for files on this server and base on support and setting this perhaps is not allow.
See PHP CUrl http://php.net/manual/en/curl.examples.php or example
http://php.net/manual/en/curl.examples.php, http://php.net/manual/en/curl.examples-basic.php
You could use cUrl as suggested by FIG-GHD742 but I find the HTTP extension a lot easier to use. It's newer and has a neat OOP api.
Another method is that you can actually do an include/require with these, but it's generally a bad idea to do so if you don't control the source from which the data is coming
It sounds like you need to enable loopback calls on the server (self-calls). It would be better to get the data on the backend if you need it on the same server. Via a PHP API or calls to a database.
**
This will help you lot : http://php.net/manual/en/curl.examples.php
http://php.net/manual/en/curl.examples.php,
http://php.net/manual/en/curl.examples-basic.php
**
Yes the above answers is right. some hosting providers disable it for security purpose. You may also try fopen(php) if you are not looking for Curl way. Read documentation here http://php.net/manual/en/function.fopen.php

How do I use external JSON...?

spent a few hours trying to figure this out, but cannot for the life of me figure out what's going wrong.
All I'm trying to do is load this:
https://recruit.zoho.com/ats/EmbedResult.hr?jodigest=2cV.Sr2As6VxhLMxQGuTNij*g.Fb3J7ysduDs.AC9sU-&atslocale=en_GB&rawdata=json
which I believe is json, into either javascript/jquery or php and use the data.
I've looked into jsonp, followed some tutorials, used some demos as templates and just can't get the above data to work.
If anyone can shed some light it would be much appreciated. It really shouldn't be this complicated, but I don't know what's going wrong.
Yep, that's JSON. The site may not support JSONP, so you're gonna have to use PHP to do this.
This is untested, but should work.
<?php
$url = 'https://recruit.zoho.com/ats/EmbedResult.hr?jodigest=2cV.Sr2As6VxhLMxQGuTNij*g.Fb3J7ysduDs.AC9sU-&atslocale=en_GB&rawdata=json';
$JSON = file_get_contents($url);
// echo the JSON (you can echo this to JavaScript to use it there)
echo $JSON;
// You can decode it to process it in PHP
$data = json_decode($JSON);
var_dump($data);
?>
JSONP relies on the server to return a JSONP formatted response. Basically, to use JSONP the server needs to return a JSON string wrapped in a function invocation ({"foo":1} becomes func({"foo":1})).
As the server your using doesn't return a JSONP response, you cannot use JSONP, you can only use JSON.
This is a shame, as JSON cannot be used x-domain due to the same origin policy (SOP). Therefore, the only option you have is to use a proxy server, which retrieves the JSON from the server, and either gives it to you in JSONP (see Yahoo Pipes), or which is on the same domain as the requested page (write a simple PHP script to get the file using file_get_contents() and then echo the output), in which case it can return the JSON.
I breifly looked at the requirements and it looks like you need an API key as well as an account. I saw that the site provides services for XML and JSON only. It looks to be fairly well documented.

using file get contents on a url that requires post data

i need to do a "file_get_contents" on a URL and there has to be data posted to the url before the contents are retrieved. is this possible? maybe something like
$contents = file_get_contents($_POST"http://www.example.com/");
so that i can then work with the $contents variable?
You cannot*** POST data using file_get_contents, you must use something like cURL
* I mark this because it is actually possible taking advantage of the third parameter which uses http context(see example one). However it really isn't worth your trouble if you have something like cURL.
Ah, I have tried to do this. Simply put you can't unless you install new extra software on your sever and go through A LOT of hassel and server load.
Best bet is to use GET if at all possible!
:)

Categories