I need to get Icecast metadata auto update by PHP let say every 15 min which will be done by cPanel cronjob.
i had the below code but it does't work(it works if I use header location to redirect however cronjob won't be able to do that)
<?PHP
$url="http://tgftp.nws.noaa.gov/data/observations/metar/stations/KJFK.TXT";
$info=file_get_contents($url);
$url_info = "http://username:password#icecast:8000/admin/metadata?mount=/mymount&mode=updinfo&song=" . urlencode($info);
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url_info);
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
?>
Try checking for errors after you execute the call using curl_error:
<?php
$url="http://tgftp.nws.noaa.gov/data/observations/metar/stations/KJFK.TXT";
$info=file_get_contents($url);
$url_info = "http://username:password#icecast:8000/admin/metadata?mount=/mymount&mode=updinfo&song=" . urlencode($info);
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url_info);
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser, check for errors
if (curl_exec($ch) === FALSE)
{
print 'Curl-Error occurred: ' . curl_error($ch).', error code: '.curl_errno($ch);
}
// close cURL resource, and free up system resources
curl_close($ch);
Related
I want to to extract buy and sell value from this website
How can I do this using file_get_contents() in PHP
For e.g
$abc = file_get_content("https://www.unocoin.com/trade?all");
Now how can I extract buy and sell value from it in every 2 min?
Here if you want to extract data from other webpage than you use php curl
Example
function curl_download($Url){
// is cURL installed yet?
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
// OK cool - then let's create a new cURL resource handle
$ch = curl_init();
// Now set some options (most are optional)
// Set URL to download
curl_setopt($ch, CURLOPT_URL, $Url);
// Include header in result? (0 = yes, 1 = no)
curl_setopt($ch, CURLOPT_HEADER, 0);
// Should cURL return or print out the data? (true = return, false = print)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Timeout in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
// Download the given URL, and return output
$output = curl_exec($ch);
// Close the cURL resource, and free system resources
curl_close($ch);
return $output;
}
print curl_download('https://www.unocoin.com/trade?all');
For this particular site, the data is json encoded so all you need to do is and it does not seem to have any further authentication requirement, as I can just get to the link and see the data.
$abc = file_get_content("https://www.unocoin.com/trade?all");
$decoded_abc=json_decode($abc);
$buy=$decoded_abc->buy;
$sell=$decoded_abc->sell;
<?php
$message=$_POST["msg"];
echo $message;
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://.../pl1.php?
m=$message");
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
?>
Above is the code iam using.anyone suggest me how do i get value of m in pl1.php
I am getting following error:
Your browser sent a request that this server could not understand.
In PHP variables passed as query string inside URL are accessible in $_GET array.
In your case it will be $_GET['m'].
Try this code:
<?php
$message=$_POST["msg"];
echo $message;
// curl initialize
$ch = curl_init();
// set curl options
curl_setopt($ch, CURLOPT_URL, "http://.../pl1.php?
m=$message");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// execute request and return response
$output = curl_exec($ch);
echo $output;
// close cURL request
curl_close($ch);
?>
And to get that data on page pl1.php use $_GET['m'];
But, I believe sending data using curl with post method is better than get method.
Thank you
Following is the call to an URL using CURL :
<?php
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
$link = $_GET['link'];
$url = "http://www.complexknot.com/user/verify/link_".$link."/";
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
?>
The variable $url contains one URL which I'm hitting using CURL.
The logic written in the file(present in a variable $url) is working absolutely fine.
After executing the code I want the control to be redirected to one URL. For it I've written following code :
header('Location: http://www.complexknot.com/login.php');
exit;
The following code is not working. The URL http://www.complexknot.com/login.php is not opening and a blank white page appears. This is the issue I'm facing.
If I don't use the CURL and hit the URL i.e. the URL contained in $url then it gets redirect to the URL http://www.complexknot.com/login.php that means header function works fine when I hit the URL in browser.
Why it's not working when I call it from CURL?
Please someone help me.
Thanks in advance.
This is happening because CURL is outputting the data. You must use curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); in order to let CURL returning data instead of outputting it.
<?php
ini_set('display_startup_errors', 0);
ini_set('display_errors', 0);
error_reporting(0);
$link = $_GET['link'];
$url = "http://www.complexknot.com/user/verify/link_$link/";
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
header('Location: http://www.complexknot.com/login.php');
You can use
<?php echo "<script>window.location.href = 'http://www.complexknot.com/login.php';</script>";die; ?>
I have a hardware which records data in every seconds. I can connect to the hardware with a browser and use the hardware’s interface and get the live data. For example: I can call the realtime data with command like this:
http://192.168.100.120:2345/realtime
That’s what I can see in the browser:
DM_NumLogChans=5
DM_NumDataModes=1
DM_LogicalChan=1
DM_ChanType=SEQUENTIAL
DM_NumDims=2
DM_DataMode=1
DM_DataModeType=TIMHIS
DM_AxisLabel.Dim1=Time
DM_AxisLabel.Dim2=Value
DM_AxisUnits.Dim1=secs
DM_AxisUnits.Dim2=microstrain
DM_SampleRate=1.000000
DM_TimeBase=0.0
DM_ChanName=bridge_1
DM_UserMin=-583.220764
DM_UserMax=940.916199
DM_Start=
-439.779 -391.875 -680.114 1001.37 0
-442.068 -396.62 -680.945 1001.37 0
-443.571 -399.705 -680.639 1001.37 0
-445.598 -404.848 -684.662 1001.37 0
A new row appear in each seconds. I would like to get this data and save it to a file or display it in real time in my php program. How can I catch the data? I tried with cURL. I think that is the solution but I am really new to this. I would appreciate any help or advice you could give me.
Try this code with your url
function curl_download($Url){
// is cURL installed yet?
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
// OK cool - then let's create a new cURL resource handle
$ch = curl_init();
// Now set some options (most are optional)
// Set URL to download
curl_setopt($ch, CURLOPT_URL, $Url);
// Set a referer
curl_setopt($ch, CURLOPT_REFERER, "http://www.example.org/yay.htm");
// User agent
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
// Include header in result? (0 = yes, 1 = no)
curl_setopt($ch, CURLOPT_HEADER, 0);
// Should cURL return or print out the data? (true = return, false = print)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Timeout in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
// Download the given URL, and return output
$output = curl_exec($ch);
// Close the cURL resource, and free system resources
curl_close($ch);
return $output;
}
echo curl_download("192.168.100.120:2345/realtime");
I am trying to retrieve users display picture through graph api and using cUrl to save it into the disk, but am unable succeed in it and getting this error when trying to check the mime type of the picture that I saved:
Notice: exif_imagetype(): Read error! in
//$userPpicture = $user_profile[picture];
//Create image instances
$url = "http://graph.facebook.com/{$userId}/picture?type=large";
$dpImage = 'temp/' . $userId . '_dpImage_' . rand().'.jpg';
echo $dpImage;
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$returned_content = get_data($url);
file_put_contents($dpImage, $returned_content);
echo "Type: " . exif_imagetype($dpImage);
for this updated code using curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); I am getting this error:
Warning: curl_setopt(): CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set in /var/fog/apps/app12345/myapp.phpfogapp.com/start.php on line 178
If this action requires any server side configuration then i might not be able to do this as am using a shared cloud storage over phpfog.
Kindly help me with this.
Thankyou.
The graph url you are using of http://graph.facebook.com/4/picture?type=large returns a HTTP 302 redirect, not the actual user image. You would need to follow the redirect and download the image at that url which is a url that looks like this: http://profile.ak.fbcdn.net/hprofile-ak-snc4/49942_4_1525300_n.jpg
As OffBySome points out, you need to follow the 302 redirect served by graph.facebook.com to the final destination, which contains the actual image data.
The simplest way to do that in this case is to add another curl_setopt call with CURLOPT_FOLLOWLOCATION as true. i.e.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true)
Check out http://us3.php.net/manual/en/function.curl-setopt.php for more details.