Connect to https://oauth.live.com/authorize with curl - php

I'm trying to get windows live connections with php. I Get it with js with w.l. sdk but not with php.
This is my code:
$CLIENT_ID =variable_get('example_contact_grabber_hotmail_client_id',NULL);
$REDIRECT_URL = variable_get('example_contact_grabber_hotmail_url_callback',NULL);
//dpm($REDIRECT_URL);
$url = "https://oauth.live.com/authorize?client_id=$CLIENT_ID&scope=wl.signin&response_type=code&redirect_uri=$REDIRECT_URL";
$url = str_replace( "&", "&", urldecode(trim($url)) );
//dpm($url);
$ch = curl_init($url);
curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, TRUE);
$salida = curl_exec($ch);
curl_close($ch);
$todo = split("\n",$salida);
//dpm($todo);
The request give me this message.
Array
(
[0] => <html><head><title>Object moved</title></head><body>
[1] => <h2>Object moved to here.</h2>
[2] => </body></html>
[3] =>
)
If I check $url in Firefox, it goes fine.

Try adding:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
//And change
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //true to false
Hope it helps

Related

Saving Facebook Profile picture from URL using cURL not working After March 27 ,2018

Im using the following code:
$json_link = "https://graph.facebook.com/v2.12/me?fields=id,picture.width(300).height(280)&access_token=".$accessToken."";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $json_link);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$output = curl_exec($ch);
curl_close($ch);
$obj = json_decode($output);
$xx=$obj->picture->data->url;
$src=imagecreatefromstring(file_get_contents($xx));
$obj returns the following:
stdClass Object (
[id] => idxxxxx
[picture] => stdClass Object (
[data] => stdClass Object (
[height] => 480
[is_silhouette] =>
[url] => lookaside.facebook.com/platform/profilepic/
[width] => 415
)
)
)
But it is failing on:
$xx=$obj->picture->data->url;
$src=imagecreatefromstring(file_get_contents($xx));
I get:
So fails:
imagecreatefromstring(file_get_contents($xx));
It worked fine before March 27, but after graph api update its no longer working.
FB is looking for User-agent information.
$User_Agent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31';
$request_headers = array();
$request_headers[] = 'User-Agent: '. $User_Agent;
$request_headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$r = curl_exec($ch);
curl_close($ch);
It will solve your problem.
Only put the line: file_put_contents('file.jpg', fopen($obj->picture->data->url, 'r'));

PHP CURL Background Process - REST API URL

I am trying to execute the background REST API Call with Curl
library in php. I guess it is not working.
can you suggest me ?
$cum_url = http://localhost/test/list;
$post = [ 'id' => $object->id ];
$ch = curl_init($cum_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 1);
curl_exec($ch);
curl_close($ch);
UPDATE: Cull error says "Timeout was reached".
Thanks,
Raja K
You need to transfer your post data from array to http parameter string, ex:
$cum_url = "http://localhost/test/list";
$post = [ 'id' => $object->id ];
$postdata = http_build_query($post);
$options = array (CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_AUTOREFERER => true,
CURLOPT_USERAGENT => "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1",
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false);
$ch = curl_init($cum_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt_array ( $ch, $options );
$res = null;
if(!curl_errno($ch)) {
$res = curl_exec($ch);
}
curl_close($ch);
Of course, some option is optional depends on you, ex CURLOPT_USERAGENT. Just show you an example.

Invalid json response on remote server

I have a very strange problem which I have been trying to figure out for 2 days, I have the below code to fetch information from a site for flight information, it works perfectly on my localhost and returns me "{thedata:what_I_Need}" but when I upload the script on my server it returns an "{invalid:true}" error from the site.
I tried it on 4 different droplets/vps, each in a different country IP with the same configuration (same curl-php-apache version and modules) as my local computer ( using ubuntu 14.4 with php5) but it still has the same issue. Here is the code which works perfectly on localhost:
$url = 'http://site.url.com';
$post = array(
'LanguageID' => 2,
'Trips' => array(array(
'Origin' => "JFK",
'Destination' => "TEX",
'Departure' => "2015-12-24T00:00:00",
)),
'Return' => "2015-12-28T00:00:00",
'Carriers' => null
'Adults' => 1,
'Children' => 0
);
$data_string = json_encode($post);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);
curl_setopt($ch,CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
curl_setopt($ch, CURLOPT_REFERER, "http://www.site.url.com");
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'X-Requested-With: XMLHttpRequest',
'Content-Length: ' . strlen($data_string))
);
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
$output=curl_exec($ch);
curl_close($ch);
I followed this article as well but I still have same issue on the remote site even if I replace CURLOPT_POST with
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);

cURL on different apache servers

This is part of my code that I use to login on a webpage (links changed):
$curl = curl_init ();
curl_setopt ( $curl, CURLOPT_URL, "loginpage");
curl_setopt ( $curl, CURLOPT_FOLLOWLOCATION, 1 );
curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $curl, CURLOPT_POST, 1 );
curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt ( $curl, CURLOPT_POSTFIELDS, "login=login&password=password");
curl_setopt ( $curl, CURLOPT_ENCODING, "" );
curl_setopt ( $curl, CURLOPT_COOKIEFILE, $_SERVER["DOCUMENT_ROOT"].'/web.cookies' );
curl_setopt ( $curl, CURLOPT_COOKIEJAR, $_SERVER["DOCUMENT_ROOT"].'/web.cookies' );
curl_setopt ( $curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)" );
curl_exec ( $curl );
curl_setopt ( $curl, CURLOPT_URL, "homepage");
$profilepage = curl_exec ( $curl );
The thing is, when I run
http://localhost/login.php
I can login and manage my web profile through my script.
The problem is, when I put all my scripts (including this one) on my external paid web server, ie.
http://myserver.com/login.php
I get a response from "loginpage" that "Cookies are not enabled".
What could possibly go wrong?

PHP Curl post with non-english characters

I have a small problem with php curl post.
I am trying to post some turkish characters to a forum but aren't posted how it should be.
This is how i save the text:
fwrite($fpp,"\xEF\xBB\xBF");
fwrite($fpp, $row['template']);
fclose($fpp);
and posting:
$this->curl = curl_init();
curl_setopt ( $this->curl, CURLOPT_URL, $this->vb_url . 'newthread.php?' . $url_vars );
curl_setopt ( $this->curl, CURLOPT_POST, true );
curl_setopt ( $this->curl, CURLOPT_POSTFIELDS, $post_fields );
curl_setopt ( $this->curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $this->curl, CURLOPT_CONNECTTIMEOUT,20);
curl_setopt ( $this->curl, CURLOPT_TIMEOUT,10);
curl_setopt ( $this->curl, CURLOPT_HEADER, true );
curl_setopt ( $this->curl, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt ( $this->curl, CURLOPT_COOKIE, $this->cookie_name );
curl_setopt ( $this->curl, CURLOPT_COOKIEJAR, $this->cookie_name );
curl_setopt ( $this->curl, CURLOPT_COOKIEFILE, $this->cookie_name );
curl_setopt ( $this->curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1');
$result = curl_exec ( $this->curl );
this is how it should be:
`Bölüm resimleri, dizi indirme ve altyazı linkine aşağıdan ulaşabilirsiniz.`
this is how it is posted:
`Bölüm resimleri, dizi indirme ve altyazı linkine aşağıdan ulaşabilirsiniz.`
Thanks
From http://php.net/manual/es/function.curl-setopt.php
Try adding:
curl_setopt($this->curl,CURLOPT_HTTPHEADER,array (
"Content-Type: application/x-www-form-urlencoded; charset=utf-8"
));
application/x-www-form-urlencoded suggested by #spencercw
Fixed with
$message = #iconv("UTF-8","Windows-1252//IGNORE",$message);

Categories