Get User Photo from Exchange server using PHP CURL - php

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

Related

cURL get html page result empty

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

cURL PHP always returns to homepage

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;
}
?>

Need assistance cURL'ing a link

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

IE and Chrome remove spaces when post data using curl

$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;
}

Equivalent of file_get_contents() with CURL?

I'm trying to get some JSON data from a url like this:
$url = 'http://site.com/search.php?term=search term here';
$result = json_decode ( file_get_contents($url) );
However the client's webhost has got the allow_url_fopen setting disabled, hence the code above doesn't work.
What's the equivalent code of the lines above? Basically, a search term needs to be submitted via $_GETto the url.
Like this:
$url = 'http://site.com/search.php?term=search term here';
$rCURL = curl_init();
curl_setopt($rCURL, CURLOPT_URL, $url);
curl_setopt($rCURL, CURLOPT_HEADER, 0);
curl_setopt($rCURL, CURLOPT_RETURNTRANSFER, 1);
$aData = curl_exec($rCURL);
curl_close($rCURL);
$result = json_decode ( $aData );
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$jsonData = curl_exec($ch);
if ($jsonData === false) {
throw new Exception('Can not download URL');
}
curl_close($ch);
$result = json_decode($jsonData);
All you need is here.
http://nadeausoftware.com/articles/2007/06/php_tip_how_get_web_page_using_curl
Basically, try something like this
function get_web_page( $url )
{
$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 => 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'] = json_decode($content);
return $header;
}
have you checked it out like this.?
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
Thanks.
// create a new cURL resource
$ch = curl_init();
// set parameters
$parameters = array(
'foo'=>'bar',
'herp'=>'derp'
);
// add get to url
$url = 'http://example.com/index.php'
$url.= '?'.http_build_query($parameters, null, '&');
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return the content
// execute
$data = curl_exec($ch);
You should then have the file contents in $data, you'll probably want to do some error checking but you can find out how to do that at php.net.

Categories