Simple Dom Html send session - php

I just want to use "file_get_html" to get content of specific pages, but this content just avaiable for loggin user, is there any way to send cookie to let target site know that the page is accessed by logged in user.
require_once 'simple_html_dom.php';
$opts = array("Cookie: __qca=P0-1170249003-1395413811270"); //__qca=P0-1170249003-1395413811270
$current_url = 'http://abc.xyz';
// But it will be redirect to
$url = 'http://www.blogger.com/blogin.g?blogspotURL=http://abc.xyz'
$context = stream_context_create($opts);
$html = file_get_html($url, FALSE, $context);
echo $html;
I do something like this, but it doesn't work.
How Can I do this with Curl?
Thanks.

$ch = curl_init(); // your curl instance
curl_setopt_array($ch, [CURLOPT_URL => "http://www.blogger.com/blogin.g?blogspotURL=http://abc.xyz", CURLOPT_COOKIE => "__qca=P0-1170249003-1395413811270"], CURLOPT_RETURNTRANSFER => true]);
$result = curl_exec($ch); // request's result
$html = new simple_html_dom(); // create new parser instance
$html->load($result); // load and parse previous result
In this example I used curl_setopt_array() to set the various CURL parameters instead of calling curl_setopt() for each one of them.
CURLOPT_URL sets the target URL, CURLOPT_COOKIE sets the cookies to send, if there are multiple cookies then they must be separated with a semicolon followed by a space, finally the CURLOPT_RETURNTRANSFER tells CURL to return the server's response as a string.
curl_exec() executes the request and returns its result.
Then we create an instance of simple_html_dom and load that previous result into it.

Thank you so much #andre, I spent all evening yesterday to find the solution :
The first we make curl exe to login to google account.
and save cookie to a text file (exam : "/tmp/cookie.txt")
and the next time everything we have to do is only take cookie content in this file to get remote content.
<?php
require_once 'simple_html_dom.php';
// Construct an HTTP POST request
$clientlogin_url = "https://www.google.com/accounts/ClientLogin";
$clientlogin_post = array(
"accountType" => "HOSTED_OR_GOOGLE",
"Email" => "youracc#gmail.com",
"Passwd" => "yourpasswd",
"service" => "blogger",
"source" => "your application name"
);
// Initialize the curl object
$curl = curl_init($clientlogin_url);
// Set some options (some for SHTTP)
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $clientlogin_post);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_COOKIEFILE, "/tmp/cookie.txt");
curl_setopt($curl, CURLOPT_COOKIEJAR, "/tmp/cookie.txt");
// Execute
$response = curl_exec($curl);
echo $response;
// Get the Auth string and save it
preg_match("/Auth=([a-z0-9_\-]+)/i", $response, $matches);
$auth = $matches[1];
echo "The auth string is: " . $auth;
// Include the Auth string in the headers
// Together with the API version being used
$headers = array(
"Authorization: GoogleLogin auth=" . $auth,
"GData-Version: 3.0",
);
$url = 'http://testlink.html';
$curl = curl_init();
// Make the request
curl_setopt($curl, CURLOPT_URL, $url );
//curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_COOKIEFILE, "/tmp/cookie.txt");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($curl);
curl_close($curl);
$html = new simple_html_dom(); // Create new parser instance
$html->load($response);
foreach($html->find('img') as $img) {
//echo $img->src . '</br>';
}

Related

Fantasy Premier League API PHP cURL

I was wondering if you could help me out with a bit of code for a cCURL request using PHP, I'm trying to retrieve data from the fpl api that would show my league standings. The url for the league standings api is - https://fantasy.premierleague.com/api/leagues-classic/my_league_id/standings/?page_new_entries=1&page_standings=1 I can see the data via the browser but when I try to retrieve it with a curl request with PHP it comes back with a 403 error with the message "Authentication credentials were not provided". That would mean that I would need login credentials to retrieve it.
After looking into it using dev tools and postman, I now know that I need to get a csrf token by logging in then save the token to use when I make the request for the league standings. I have no idea how to go about this, I kind of do but I would really appreciate if someone could give it a go for me.
What I would need to do is make a POST request to https://users.premierleague.com/accounts/login/ with this form data -
"login" => "my_email",
"password" => "my_password",
"app" => "plfpl-web",
"redirect_uri" => "https://fantasy.premierleague.com/",
After making the request I would need to capture the csrf token cookie, which I believe would be in the hidden input with the name - "csrfmiddlewaretoken" and save it in a variable.
Once getting the token and saving it, I would then make a GET request to https://fantasy.premierleague.com/api/leagues-classic/my_league_id/standings/ with placing the csrf token variable that I saved into the headers and then json decode that data so I'm able to echo out the league details.
I'm pretty sure that's how to do it but I'm not that great at PHP and was wondering if there is any savour out there that could help a brother out. Any help would be much appreciated :)
I've started with the first part, making the initial post request, but have had no luck in returning the token. Here's my code so far -
<?php
$cookie = "cookies.txt";
$url = 'https://users.premierleague.com/accounts/login/';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
// var_dump($response);
$dom = new DOMDocument;
#$dom->loadHTML($response);
$tags = $dom->getElementsByTagName('input');
for($i = 0; $i < $tags->length; $i++) {
$grab = $tags->item($i);
if($grab->getAttribute('name') === 'csrfmiddlewaretoken') {
$token = $grab->getAttribute('value');
}
}
echo $token;
?>
<?php
// id of the league to show
$league_id = "your_league_id";
// set the relative path to your txt file to store the csrf token
$cookie_file = realpath('your_folder_dir_to_the_txt_file/cookie.txt');
// login url
$url = 'https://users.premierleague.com/accounts/login/';
// make a get request to the official fantasy league login page first, before we log in, to grab the csrf token from the hidden input that has the name of csrfmiddlewaretoken
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
$dom = new DOMDocument;
#$dom->loadHTML($response);
// set the csrf here
$tags = $dom->getElementsByTagName('input');
for($i = 0; $i < $tags->length; $i++) {
$grab = $tags->item($i);
if($grab->getAttribute('name') === 'csrfmiddlewaretoken') {
$token = $grab->getAttribute('value');
}
}
// now that we have the token, use our login details to make a POST request to log in along with the essential data form header fields
if(!empty($token)) {
$params = array(
"csrfmiddlewaretoken" => $token,
"login" => "your_email_address",
"password" => "your_password",
"app" => "plfpl-web",
"redirect_uri" => "https://fantasy.premierleague.com/",
);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
/**
* using CURLOPT_SSL_VERIFYPEER below is only for testing on a local server, make sure to remove this before uploading to a live server as it can be a security risk.
* If you're having trouble with the code after removing this, look at the link that #Dharman provided in the comment section.
*/
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//***********************************************^
$response = curl_exec($ch);
// set the header field for the token for our final request
$headers = array(
'csrftoken ' . $token,
);
}
// finally, we now have everything we need to make the GET request to retrieve the league standings data. Enjoy :)
$fplUrl = 'https://fantasy.premierleague.com/api/leagues-classic/' . $league_id . '/standings/';
curl_setopt($ch, CURLOPT_URL, $fplUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
if(!empty($token)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
$response = curl_exec($ch);
$league_data = json_decode($response, true);
curl_close($ch);
echo '<pre class="card">';
print_r($league_data);
echo '</pre>';
?>

PUT Request via CURL

I'm trying to make an CURL PUT REQUEST, my code looks like:
public function assignProfile($token, $organizationId, $profileId)
{
//define enviroment and path
$host = enviroment;
$path = "/admin/organizations/".$organizationId."/users";
$data_string = '["'.$profileId.'"]';
// set up the curl resource
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host.$path);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer '.$token.'',
'Content-Length: ' . strlen($data_string)
));
echo "<br>OK<br>";
// execute the request
$output = curl_exec($ch);
// return ID for a new case
$output = json_decode($output);
var_dump($output);
}
Each part looks correct, when I var_dump $path, $host, even $data_string looks correct. However var_dump() at the end throw just NULL
I expect I'm doing something wrong or missing something really important.
May I ask you for some advise?
Thanks
EDIT:
What i do with it:
// define
define("Audavin","here is some uniqe ID");
.
.
.
$Users = new Users;
// this return Auth token ( I verify this work with echo )
$token = $Users->authorization();
// Calling method mentioned above
$Users->assignProfile($token,"here is org id", Audavin);
I would start by making sure the URL you're making the request to actually works and returns a valid response, you can do so by using a simple REST client (like POSTMAN chrome extension for example)
If you do get a response back, try and see if it's indeed a valid JSON, if not, that could be why you're not getting anything back from json_decode (more on return values here: http://php.net/manual/en/function.json-decode.php)
Finally, It is suggested you add curl_close($ch) to the end of your code to make sure your release the curl handle.

Google Cloud Storage - Upload an 'Object' without using Google Client

I've been looking around for the past few days for an answer to this question, but I haven't been able to find the solution I'm after.
I have a fully working version of doing this task using Google_Client, however I want to be able to do this without using the Google_Client. I can create buckets without using it, but not Objects. I cannot understand the Google Documentation on this at all (it's pretty poor).
So, I have a function below which takes in several parameters to try and upload a file. I'm not sure what needs to go in $authheaders, $post_fields (the body of the post) or the url.
public function upload_file($bucket, $fileName, $file, $fileType) {
// Check if we have auth token
if(empty($this->authtoken)) {
echo "Please login to Google";
exit;
}
// Prepare authorization headers
$authheaders = array(
"Authorization: Bearer " . $this->authtoken
);
$postbody = array();
//Http call for creating a file
$response = $this->http_call(self::FILE_UPLOAD_URL.$bucket.'/o?uploadType=multipart&name='.$fileName, $postbody, $authheaders);
// Has the file been created successfully?
if($response->success=="1") {
return array('status' => 'success', 'errorcode' =>'', 'errormessage'=>"", 'id' => $response->job->id);
}
else {
return $response;
}
}
The http_call function:
public function http_call($url, $post_fields, $authheaders) {
// Make http call
$this->httpRequest->setUrl($url);
$this->httpRequest->setPostData(json_encode($post_fields));
$this->httpRequest->setHeaders($authheaders);
$this->httpRequest->send();
$response = json_decode($this->httpRequest->getResponse());
return $response;
}
Any help on this would be greatly appreciated.
If anyone else is looking to do this without using the API, here is how I solved my question using curl.
// Prepare authorization headers
$authheaders = array(
"Authorization: Bearer " . $this->authtoken,
"Content-Type: application/pdf"
);
$url = self::FILE_UPLOAD_URL.$bucket.'/o/?uploadType=multipart&name='.$fileName.'';
$curl = curl_init();
curl_setopt($curl, CURLOPT_POSTFIELDS, $file);
curl_setopt($curl, CURLOPT_HTTPHEADER, $authheaders);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_USERAGENT, "Goole Cloud Storage PHP Starter Application google-api-php-client/1.1.5");
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
// 1 is CURL_SSLVERSION_TLSv1, which is not always defined in PHP.
curl_setopt($curl, CURLOPT_SSLVERSION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
$response = curl_exec($curl);

How do I make a POST using X-HTTP-Method-Override with a PHP curl request?

I'm working with the Google Translate API and there's the possibility that I could be sending in quite a bit of text to be translated. In this scenerio Google recommends to do the following:
You can also use POST to invoke the API if you want to send more data
in a single request. The q parameter in the POST body must be less
than 5K characters. To use POST, you must use the
X-HTTP-Method-Override header to tell the Translate API to treat the
request as a GET (use X-HTTP-Method-Override: GET). Google Translate API Documentation
I know how to make a normal POST request with CURL:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($curl);
curl_close($curl);
echo $response;
But how do I modify the header to use the X-HTTP-Method-Override?
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: GET') );
http://php.net/manual/en/function.curl-setopt.php
CURLOPT_HTTPHEADER
An array of HTTP header fields to set, in the format array('Content-type: text/plain', 'Content-length: 100')
Thus,
curl_setopt($curl, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: GET'));
use the CURLOPT_HTTPHEADER option to add a header from a string array
Not enough for me , i need to use http_build_query fo my array post data
my full example :
$param = array(
'key' => 'YOUR_API_KEY_HERE',
'target' => 'en',
'source' => 'fr',
"q" => 'text to translate'
);
$formData = http_build_query($param);
$headers = array( "X-HTTP-Method-Override: GET");
$ch=curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$formData);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers );
curl_setopt($ch, CURLOPT_REFERER, 'http://yoursite'); //if you have refere domain restriction for your google API KEY
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,'https://www.googleapis.com/language/translate/v2');
$query = curl_exec($ch);
$info = curl_getInfo($ch);
$error = curl_error($ch);
$data = json_decode($query,true);
if (!is_array($data) || !array_key_exists('data', $data)) {
throw new Exception('Unable to find data key');
}
if (!array_key_exists('translations', $data['data'])) {
throw new Exception('Unable to find translations key');
}
if (!is_array($data['data']['translations'])) {
throw new Exception('Expected array for translations');
}
foreach ($data['data']['translations'] as $translation) {
echo $translation['translatedText'];
}
I found this help here https://phpfreelancedeveloper.wordpress.com/2012/06/11/translating-text-using-the-google-translate-api-and-php-json-and-curl/
Hope that helps

How to get facebook token code via curl?

I tried use easy curl to get facebook code and access_token via curl. So that I can post message to my wall. I know sdk, but if I only use easy curl way?
some fault return:
Method Not Implemented
Invalid method in request
Here is my code. BY THE WAY: How to remember a access_token if it is still valid in 1 hour period time? Thanks.
$code_url = "https://graph.facebook.com/oauth/authorize?client_id=".$app_id."&redirect_uri=".urlencode($canvas_page_url)."&type=client_cred&display=page&scope=user_photos,publish_stream,read_stream,user_likes";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$code_url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1);
curl_setopt($ch, CURLOPT_HEADER ,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,1);
$fb_code = curl_exec($ch); // get code
curl_close($ch);
$token_url = "https://graph.facebook.com/oauth/access_token?client_id=".$app_id."&client_secret=".$app_secret."&redirect_uri=".urlencode($canvas_page_url)."&code=".$fb_code."";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$token_url);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION ,1);
curl_setopt($curl, CURLOPT_HEADER ,0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER ,1);
$result = curl_exec($curl);
curl_close($curl);
echo $result; //get token for post to wall
EDIT:
$app_id = "14XXXXXXXX";
$app_secret = "77XXXXXXXXXXXXXXXXXXXXXXX";
function get_app_access_token($app_id, $secret) {
$url = 'https://graph.facebook.com/oauth/access_token';
$token_params = array(
"type" => "client_cred",
"client_id" => $app_id,
"client_secret" => $app_secret
);
return str_replace('access_token=', '', post_url($url, $token_params));
}
$token = get_app_access_token($app_id,$app_secret);
echo $token;
Try these helper methods which will make a cURL call to the graph API and return an application access_token.
This function takes a url and an array of parameters and makes a POST via cURL:
function post_url($url, $params) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params, null, '&'));
$ret = curl_exec($ch);
curl_close($ch);
return $ret;
}
This function takes your app ID and secret provided in the developer app and returns an active access_token for your app:
function get_app_access_token($app_id, $secret) {
$url = 'https://graph.facebook.com/oauth/access_token';
$token_params = array(
"type" => "client_cred",
"client_id" => $app_id,
"client_secret" => $secret
);
return str_replace('access_token=', '', post_url($url, $token_params));
}
You can call the method like so $token = get_app_access_token('APP_ID','SEKRET');
They really ought to make this clearer on the FB guide, but here's what worked for me. Unlike what a lot of people are saying online, you do NOT do a GET curl call but you still do a POST call.
So, the URL you post to will be "https://graph.facebook.com/oauth/access_token?"
And then, your POST variables that you'll feed CURLOPT_POSTFIELDS will be something like:
"client_id=YOUR_CLIENT_ID&redirect_uri=" . urlencode("YOUR_URL") . "&client_secret=YOUR_CLIENT_SECRET&code=YOUR_RECEIVED_CODE"
I was literally trying for at least 1.5 hours trying to get this to work. Every time I went to the URL in a browser it was fine, and every time I tried to run a GET curl command, or a POST command where all the variables were still in the url (and not specified in POSTFIELDS) it failed. It was really annoying.

Categories