SHOUTcast XML get with php - php

I have shoutcast administrator and i need to read xml from that it looks like this
http://SHOUTCAST-IP:PORT/admin.cgi
And i need to login and get the XML Data from http://SHOUTCAST-IP:PORT/admin.cgi?mode=viewxml and do it with php, i have made this script
$fp = fsockopen($server, $port, $errno, $errstr, 30);
fputs($fp, "GET /admin.cgi?pass=".$password."&mode=viewxml HTTP/1.0\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)\r\n\r\n"); //
while (!feof($fp)) {
$content = fgets($fp);
}
But it doesn't work, it says Unauthorised. How can i fix it ?

Are you sure you can use standard $_GET variables to provide username and password there? Seems quite bad practice!?
I would use cURL to achieve this, it's quick and easy!
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://SHOUTCAST-IP:PORT/admin.cgi");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "user:pwd");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, 1);
$output = curl_exec($ch);
curl_close($ch);

What about this?
//*********** YOUR FM ***********//
// Begin Configuration
$scdef = "YOUR FM";
// ABOVE: Default station name to display when server or stream is down
$scip = "127.0.0.1"; // ip or url of shoutcast server (DO NOT ADD HTTP:// don't include the port)
$scport = "5977"; // port of shoutcast server
$scpass = "mysecretpassword"; // password to shoutcast server
// End Configuration
//*********** YOUR FM ***********//
$scfp = fsockopen("$scip", $scport, &$errno, &$errstr, 30);
if(!$scfp) {
$scsuccs=1;
echo''.$scdef.' is Offline';
}
if($scsuccs!=1){
fputs($scfp,"GET /admin.cgi?pass=$scpass&mode=viewxml HTTP/1.0\r\nUser-Agent: SHOUTcast Song Status (Mozilla Compatible)\r\n\r\n");
while(!feof($scfp)) {
$page .= fgets($scfp, 1000);
}
// REST OF YOUR CODE BELOW HERE!

Related

Alternative of fsockopen with curl in php

I am trying to call a web service of a shipping company in my php code and get the result xml. I have this sample code and i want to know if there is an alternative using curl.
Code:
function doPost($_postContent) {
$postContent = "xml_in=".$_postContent;
$host="test.company.com";
$contentLen = strlen($postContent);
$httpHeader ="POST /shippergate2.asp HTTP/1.1\r\n"
."Host: $host\r\n"
."User-Agent: PHP Script\r\n"
."Content-Type: application/x-www-form-urlencoded\r\n"
."Content-Length: $contentLen\r\n"
."Connection: close\r\n"
."\r\n";
$httpHeader.=$postContent;
$fp = fsockopen($host, 81);
fputs($fp, $httpHeader);
$result = "";
while(!feof($fp)) {
// receive the results of the request
$result .= fgets($fp, 128);
}
// close the socket connection:
fclose($fp);
$result = explode("\r\n\r\n", $result,3);
}
Can i call it using curl?
You can use the CURLOPT_PORT option to change the port to 81. See http://php.net/manual/en/function.curl-setopt.php
$url = "http://test.company.com/shippergate2.asp";
$ch = curl_init();
curl_setopt($ch, CURLOPT_PORT, 81);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, "PHP Script");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postContent);
$data = curl_exec($ch);
I guess a complete solution is needed but I suggest checking this basic CURL wrapper for PHP https://github.com/shuber/curl

Specify source port range with curl using PHP

I need to specify the source port range for curl.
I don't see any option who let me choose a range for source port in TCP.
Is it possible?
You can use CURLOPT_LOCALPORT and CURLOPT_LOCALPORTRANGE options, which are similar to curl's --local-port command line option.
In the following example curl will try to use source port in range 6000-7000:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_LOCALPORT, 6000);
curl_setopt($ch, CURLOPT_LOCALPORTRANGE, 1000);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
From the command line one would use:
curl --local-port 6000-7000 <url>
For documentation see: CURLOPT_LOCALPORT and Local port number.
I think it would be better using fsockopen. I came up many times this worked for me when blocked by firewalls. See: http://php.net/fsockopen
$ports = array(80, 81);
foreach ($ports as $port) {
$fp =# fsockopen("tcp://127.0.0.1", $port);
// or fsockopen("www.google.com", $port);
if ($fp) {
print "Port $port is open.\n";
fclose($fp);
} else {
print "Port $port is not open.\n";
}
}
By the way, there is CURLOPT_PORT for CURL, but doesn't work with tcp://127.0.0.1;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://127.0.0.1");
curl_setopt($ch, CURLOPT_PORT, 80);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$re = curl_exec($ch);
// echo curl_errno($ch);
curl_close($ch);
print $re;

SSL Issue, Logging in to website

I have this code to login to a server called Pinger TextFree for a bot I'm working on:
<?php
function sendRequest($url, $postorget, $fields = array(), $proxy)
{
$cookie_file = "cookies.txt";
//Initiate connection
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); // set url
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_URL, $url); // set url
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return the transfer
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // allow https
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)'); // random agent
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // automatically follow Location: headers (ie redirects)
curl_setopt($ch, CURLOPT_AUTOREFERER, 1); // auto set the referer in the event of a redirect
curl_setopt($ch, CURLOPT_MAXREDIRS, 5); // ibm likes to redirect a lot, make sure we dont get stuck in a loop
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); // file to save cookies in
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); // file to read cookies from
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 40); //timeout time for curl
curl_setopt($ch, CURLOPT_PORT, 80); //port to connect to (default 80 obviously)
//Check to see if a proxy is being used
if(isset($proxy)){
//Tell cURL you're using a proxy
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
//Set the proxy
curl_setopt($ch, CURLOPT_PROXY, $proxy);
}
//Check if request is POST or GET
if ($postorget == "post" OR $postorget == "POST")
{
curl_setopt($ch, CURLOPT_POST, true); // use POST
if (is_array($fields)){
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields)); // key => name gets turned into &key=name
} else {
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); // &key=name passed in
}
} else {
curl_setopt($ch, CURLOPT_POST, false); // use GET
}
$content = curl_exec($ch); // return html content
$info = curl_getinfo($ch); // return transfer info
$error = curl_error($ch); // return any errors
curl_close($ch);
$request = array('content' => $content,
'error' => $error,
'info' => $info);
return $request;
}
//Login details
$username = "usernamehere";
$password = "passwordhere";
//GET the initial login page
$initFields = "";
$initOutput = sendRequest("http://www.pinger.com/tfw/?t=1360619019053", "GET", $initFields);
echo "<textarea cols='100' rows='400'>";
print_r($initOutput);
echo "</textarea>";
//Login to pinger
$loginFields = "{\"username\":\"".$username."\",\"password\":\"".$password."\",\"clientId\":\"textfree-in-flash-web-free-1360619009-8CA1C5C1-38ED-2E31-3248-CB367450A20F\"}";
$loginOutput = sendRequest("https://api.pinger.com/1.0/web/login", "POST", $loginFields);
echo "<textarea cols='100' rows='400'>";
print_r($loginOutput);
echo "</textarea>";
?>
For some reason every time I try to run this script all I get is "Unknown SSL protocol error in connection to api.pinger.com:80"
What am I doing wrong here? I'll even specify SSL2 in the setop but it just hangs forever - I just can't get this to work!
Here is the app I'm trying to automate: http://www.pinger.com/tfw/
It's in flash but I'm using Fiddler to sniff the HTTP/HTTPS requests to automate them with cURL.
Any ideas from you guys?
The error is obviously in the line
curl_setopt($ch, CURLOPT_PORT, 80); //port to connect to (default 80 obviously)
HTTPS servers listen on port 443 by default. Simply deleting this line should be sufficient; curl will then figure out the port from the protocol in the URL.

how can i see url status code in PHP with out using curl option?

I want to create site map for my site. So before creating sitemap, i want to know the status code of each url. I have used curl option to deduct status code. I have more than 400 urls in my site. if i use curl, its taking long time.
Only i want to allow the url which is contain status code 200.
Could you please any one tell me any other option to deduct each url's status code.
I have used below curl code.
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $urlparam);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 240);
curl_exec($ch);
$curlcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo $curlcode;
reference link
I ran into this same issue a few months ago. I found that using this code example to access my own page status codes was much faster:
<?php
//
// Checking the status of a web page - funmin.com
//
$server="www.YOUR_WEBSITE.com";
function sockAccess($page)
{
$errno = "";
$errstr = "";
$fp = 0;
global $server;
$fp = fsockopen($server, 80, $errno, $errstr, 30);
if ($fp===0)
{
die("Error $errstr ($errno)");
}
$out = "GET /$page HTTP/1.1\r\n";
$out .= "Host: $server\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp,$out);
$content = fgets($fp);
$code = trim(substr($content,9,4));
fclose($fp);
return intval($code);
}
?>
Further documentation may be found here: http://www.forums.hscripts.com/viewtopic.php?f=11&t=4217

CURL server data transfer timeout

I am using the following code to get the xml data form icecat.biz:
set_time_limit (0);
$login = "Arpan";
$password = "arpan";
//$url="http://data.icecat.biz/export/freexml.int/EN/files.index.xml";
$url= "http://data.icecat.biz/export/level4/EN";
//$url="http://data.icecat.biz/export/freexml.int/DE/10.xml";
$user_agent = 'Mozilla/5.0 (Windows; U;
Windows NT 5.1; ru; rv:1.8.0.9) Gecko/20061206 Firefox/1.5.0.9';
$header = array(
"Accept: text/xml,application/xml,application/xhtml+xml,
text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
"Accept-Language: ru-ru,ru;q=0.7,en-us;q=0.5,en;q=0.3",
"Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7",
"Keep-Alive: 300");
$local_path = "myxml.xml";
$file_handle = fopen($local_path, "w");
ob_start();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FILE, $file_handle);
curl_setopt($ch, CURLOPT_HEADER, 0);
//curl_setopt ( $ch , CURLOPT_HTTPHEADER, $header );
curl_setopt($ch, CURLOPT_USERPWD, $login . ":" . $password);
curl_setopt($ch, CURLOPT_TIMEOUT, 0); // times out after 4s
//curl_setopt($c, CURLOPT_TIMEOUT, 2);
//curl_setopt($ch, CURLOPT_NOBODY, TRUE); // remove body
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//$head = curl_exec($ch);
$result = curl_exec ($ch);
if(curl_errno($ch))
{
echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);
ob_end_clean();
fclose ($file_handle);
$xmlStr = file_get_contents($local_path);
$xmlObj = simplexml_load_string($xmlStr);
print "<pre>";
//print_r($xmlObj->Product->ProductRelated->attributes()->ID);
print_r($xmlObj);
exit;
The page is being executed for a unlimited time but the XML is not being updated after 10 to 20 sec. The output xml is also not being completed. I think after a certain time the server is not responding or data is not being transferred.
Here is the error message:
**** The server xml (icecat) size is big
What is the problem and how do I fix it?
Sounds like you are not giving enough time for the request to download properly.
Uncomment your //curl_setopt($c, CURLOPT_TIMEOUT, 2);
And put the timeout to 600 for a test.
Beyond that your request looks fine, you could always check to see if the server is caching responses, the last thing that I've seen recently happening is if some of my users have reverse proxies in order to cache their normal operations. Where some truncated responses got cached and thats all they got back for a 24 hour period although that may not be related to you.

Categories