I am trying to curl the following link:
http://soundstreamradio.serverroom.net:7424/currentsong?sid=1
It's just a page that displays plain text. This is the cURL PHP code I'm using:
function get_web_page( $url )
{
$user_agent='Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0';
$options = array(
CURLOPT_CUSTOMREQUEST =>"GET", //set request type post or get
CURLOPT_POST =>false, //set to GET
CURLOPT_USERAGENT => $user_agent, //set user agent
CURLOPT_COOKIEFILE =>"cookie.txt", //set cookie file
CURLOPT_COOKIEJAR =>"cookie.txt", //set cookie jar
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
return $header;
}
And this is what I'm using to call the script:
$result = get_web_page("http://soundstreamradio.serverroom.net:7424/index.html?sid=1");
if ( $result['errno'] != 0 )
echo "... error: bad url, timeout, redirect loop ...";
if ( $result['http_code'] != 200 )
echo"... error: no page, no permissions, no service ...";
$page = $result['content'];
Now after uploading this to my server and running it, the page just hangs and does nothing, then after the 120 seconds it times out finally displaying all of the error messages (... error: bad url, timeout, redirect loop ..., ... error: no page, no permissions, no service ...)
I'm looking to see if there are any options I can take to pull the data off of this website without the cross domain issues.
Try
echo file_get_contents("http://soundstreamradio.serverroom.net:7424/currentsong?sid=1");
Use curl_exec and other functions for https connections
This is working for me - plain and simple
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://soundstreamradio.serverroom.net:7424/currentsong?sid=1');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
this also works
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://soundstreamradio.serverroom.net/currentsong?sid=1');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_PORT , 7424);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
if neither of these work then I would suggest looking at your firewall as you are probaly being blocked on port 7424
Related
I have some code to that should get the webpage from a supplied url. The urls will look something like this:
https://www.facebook.com/events/383049449109054/
This is the code i have for fetching the page:
function GetHtmlContents($url){
echo $url;
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
echo "<br>";
echo $data;
}
but when i run this from my localhost the echo $data is empty it doesnt show anything.
How do i fix this? This is my first time working with cURL.
You can use this code to get page content
"
function GetHtmlContents( $url )
{
$user_agent="Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0";
$options = array(
CURLOPT_CUSTOMREQUEST =>"GET", //set request type post or get
CURLOPT_POST =>false, //set to GET
CURLOPT_USERAGENT => $user_agent, //set user agent
CURLOPT_COOKIEFILE =>"cookie.txt", //set cookie file
CURLOPT_COOKIEJAR =>"cookie.txt", //set cookie jar
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
return $header;
} print_r(GetHtmlContents('https://www.facebook.com/events/383049449109054/'));
"
Try this, it should work for you.
function GetHtmlContents($url){
echo file_get_contents($url);
}
Read more about file_get_contents()
http://php.net/manual/en/function.file-get-contents.php
I am trying to get and save an image from a webpage to my pc, but when I pass its url by grabbing it form page and save it in variable. and pass that variable to my curl image fetch function it returns 404.
But when I print that grabbed url variable and pass that printed value of variable to function by copy pasting , it returns 200(OK) and saves the image.
Here is my code:
preg_match('/id=\"searchForm:j_idt40\" src=\"(.*?)\"/', open('http://pas.fdle.state.fl.us/pas/restricted/PAS/StolenBoats.jsf'), $matches);
$url2= "http://pas.fdle.state.fl.us".$matches[1];
echo $url2;
grab_image($url2,"image2.jpg");
function open($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.75 Safari/537.36');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_COOKIE, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, "1"); //writes cookie
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_REFERER, 'http://pas.fdle.state.fl.us/pas/restricted/PAS/StolenBoats.jsf');
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
function get_web_page( $url )
{
$user_agent='Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0';
$options = array(
CURLOPT_CUSTOMREQUEST =>"GET", //set request type post or get
CURLOPT_POST =>false, //set to GET
CURLOPT_USERAGENT => $user_agent, //set user agent
CURLOPT_COOKIEFILE =>"cookie.txt", //set cookie file
CURLOPT_COOKIEJAR =>"cookie.txt", //set cookie jar
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
$code=curl_getinfo($ch,CURLINFO_HTTP_CODE);
echo"<br>resp code:".$code;
curl_close( $ch );
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
return $header;
}
function grab_image($url1,$save) {
echo"<br>#########################################################<br>";
echo"URL=".$url1;
$result = get_web_page($url1);
echo"<br>Errorno:".$result['errno'];
echo"<br>Error msg:".$result['errmsg'];
echo"<br>Content:".$result['content'];
if(file_exists($save)){
unlink($save);
}
$fp = fopen($save,'x');
fwrite($fp, $result['content']);
fclose($fp);
}
Output to this is:
http://pas.fdle.state.fl.us/pas/javax.faces.resource/dynamiccontent.properties.jsf;jsessionid=yJ3Orpy4CG94G60BI1kpm43CSmuV8TYUOFa3IRr_.MEGADETHPRIMARY?ln=primefaces&v=6.1.2&pfdrid=558b6b95c559b790d2a10e03dbb80110&pfdrt=sc&pfdrid_c=false&uid=431a2d2a-970e-4584-9c6b-c7f88fb30b38
#########################################################
URL=http://pas.fdle.state.fl.us/pas/javax.faces.resource/dynamiccontent.properties.jsf;jsessionid=yJ3Orpy4CG94G60BI1kpm43CSmuV8TYUOFa3IRr_.MEGADETHPRIMARY?ln=primefaces&v=6.1.2&pfdrid=558b6b95c559b790d2a10e03dbb80110&pfdrt=sc&pfdrid_c=false&uid=431a2d2a-970e-4584-9c6b-c7f88fb30b38
resp code:404
Errorno:0
Error msg:
Content:
#########################################################
var_dump($url) results are:
dump before calling function:
string(297) "http://pas.fdle.state.fl.us/pas/javax.faces.resource/dynamiccontent.properties.jsf;jsessionid=QoecD0pG5uUUzXKOtMalTPJQhVu8ZkZFN7Y2VWYo.MEGADETHPRIMARY?ln=primefaces&v=6.1.2&pfdrid=558b6b95c559b790d2a10e03dbb80110&pfdrt=sc&pfdrid_c=false&uid=f15b2a89-ae8e-49d5-ac70-933c026b1ecb"
dump before curl execution inside function
string(297) "http://pas.fdle.state.fl.us/pas/javax.faces.resource/dynamiccontent.properties.jsf;jsessionid=QoecD0pG5uUUzXKOtMalTPJQhVu8ZkZFN7Y2VWYo.MEGADETHPRIMARY?ln=primefaces&v=6.1.2&pfdrid=558b6b95c559b790d2a10e03dbb80110&pfdrt=sc&pfdrid_c=false&uid=f15b2a89-ae8e-49d5-ac70-933c026b1ecb"
resp code:404
dump after curl:
string(297) "http://pas.fdle.state.fl.us/pas/javax.faces.resource/dynamiccontent.properties.jsf;jsessionid=QoecD0pG5uUUzXKOtMalTPJQhVu8ZkZFN7Y2VWYo.MEGADETHPRIMARY?ln=primefaces&v=6.1.2&pfdrid=558b6b95c559b790d2a10e03dbb80110&pfdrt=sc&pfdrid_c=false&uid=f15b2a89-ae8e-49d5-ac70-933c026b1ecb"
I am trying to get the cURL to reload a new page after I click a link but the only thing that change is the url and not the page it self (look below to see what I mean)
https://i.stack.imgur.com/hXQ4U.png
https://i.stack.imgur.com/MxUuw.png
Here is my code
<?php
//create a new cURL resource
$ch = curl_init();
//set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "https://www.google.com");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
// grab URL and pass it to the browser
$output = curl_exec($ch);
if ($output === FALSE) {
echo "cURL Error: " . curl_error($ch);
}
//close cURL resource, and free up system resources
curl_close($ch);
print_r($output);
/**
* Get a web file (HTML, XHTML, XML, image, etc.) from a URL. Return an
* array containing the HTTP server response header fields and content.
*/
function get_web_page( $url )
{
$user_agent='Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0';
$options = array(
CURLOPT_CUSTOMREQUEST =>"GET", //set request type post or get
CURLOPT_POST =>false, //set to GET
CURLOPT_USERAGENT => $user_agent, //set user agent
CURLOPT_COOKIEFILE =>"cookie.txt", //set cookie file
CURLOPT_COOKIEJAR =>"cookie.txt", //set cookie jar
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
return $header;
}
?>
I am trying to get user profile picture located in Microsoft Exchange Server [version 2013].
I can see the picture using below url [Example] :
https://companydomain.com/owa/service.svc/s/GetPersonaPhoto?email=user1%40companydomain.com&size=HR96x96&sc=1464941029314
Requirement:
To get all the company user's profile picture downloaded to a folder.
What I have done so far?
Written a Script using Php CURL
<?php
$user = 'user1#companydomain.com';
$password = 'XXXXXXX';
$fullurl="https://companydomain.com/owa/service.svc/s/GetPersonaPhoto?email=user1%40companydomain.com&size=HR96x96&sc=1464941029314";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FAILONERROR, 0);
curl_setopt($ch, CURLOPT_USERPWD, "$user:$password");
curl_setopt($ch, CURLOPT_URL, $fullurl);
$returned = curl_exec($ch);
curl_close ($ch);
Output:
A page containing: Object moved to here. as text and when I clicked on the link "here" it goes to login page of Microsoft Outlook.
Please help me to get desired out put and let me know what I am missing.
Thanks in advance
It seems that you get redirected (301 or 302) at the URL you rovided. I can provide a solution, qhich provides you the "target"-URL which should be the needed Image.
function get_web_page( $url )
{
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => true, // return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "boss", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
//$header['errno'] = $err;
// $header['errmsg'] = $errmsg;
//$header['content'] = $content;
print($header[0]);
return $header;
}
$thisurl = "http://www.example.com/redirectfrom";
$myUrlInfo = get_web_page( $thisurl );
echo $myUrlInfo["url"];
you should get now http://www.example.com/redirectto
$first1 = $_POST['CFirst'];
$last1 = $_POST['CLast'];
$Phone1 =$_POST['Phone'];
$Fax1 = $_POST['Fax'];
$CEmail1 = $_POST['CEmail'];
$message1 = $_POST['Message']
$post_string = "ID=$id&Source=$Source&noMail=$noMail&CFirst=$first1&CLast=$last1&Phone=$Phone1&CEmail=$CEmail1&Message=$message1";
//create cURL connection
$curl_connection =
curl_init($url);
//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT,
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
The problem with the above code is that it removes spaces in chrome and IE but in case of Firefox its working fine.
e.g:-
Lets says if i put spaces in my input field 'Hello how are you'. now right now its going as Hellohowareyou in case of IE/chrome. What can be the problem?
Any ideas?
I am not sure if this helps, but just sharing a function I have written to get pages using curl.
function get_web_page($url)
{
//echo "curl:url<pre>".$url."</pre><BR>";
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 15, // timeout on connect
CURLOPT_TIMEOUT => 15, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
);
$ch = curl_init($url);
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch,CURLINFO_EFFECTIVE_URL );
curl_close( $ch );
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
//change errmsg here to errno
if ($errmsg)
{
echo "CURL:".$errmsg."<BR>";
}
return $content;
}