I have a PHP Code, which reloads itself with another GET value. Like: example.com?number=453 and it keeps doing this for days. I was doing this in the browser. But i found cron job is way better.
So, I need to use CURL to reload the page with a new GET value like ?number=550. So this is the code, which i use (found it on stackoverflow)
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;
}
$output = curl_download("http://www.example.com/yourscript.php?number='$requestsDone'");
And at the end of the page, i use this, to call the function
curl_download($Url);
But i am getting this error:
Notice: Undefined variable: Url
in the last line, i.e curl_download($Url);
This is how we call the function right? What's wrong? Also is there any mistake or improvement, I can make in the code?
You not assign values to variables $Url.
Before you can assign values to variables $Url. That like $Url = "http://domaintest.com/?number=550";
curl_download($Url);
Related
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);
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;
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");
Hi I'm trying to use this script to return a value from another php script on my site without leaving the page, but I am getting the number 1 after the string (I'm assuming that the 1 is the resource number) This is the script:
<?php
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, false);
// grab URL and pass it to the browser
$result = curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
echo $result;
?>
Output is somthing like this:
"<?php include \\\\localfolder\\blabla\script.php ?>1"
and the 1 at the end is what I don't want showing up.
Thanks,
Robert
You need to set the CURLOPT_RETURNTRANSFER option to true:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
From the documentation:
CURLOPT_RETURNTRANSFER
TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
I would like to use Email Yak to send and receive emails with PHP. Is this posssible? If so how would I go about it?
I am not sure why you would want to do this from PHP, but take a look at the 'Send Email' manual page at the Email Yak project.
You will need to use PHPs curl extension to make the HTTP requests with the appropriate headers and PHPs native json_encode() function to serialise the data you are sending.
A really simple curl example from jonasjohn.de to get you going:
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;
}
Email Yak uses GET, POST and JSON. Definitely you can integrate with PHP.
See http://docs.emailyak.com/ for more information.