Facebook Graph API call returns false - php

I'm an amateur programmer trying to fetch my facebook page likes for my webpage since I do not want facebooks standard plugin. After a lot of research and testing I found out the right call to make which is:
https://graph.facebook.com/{PAGE_ID}?access_token={APP_ID}|{APP_SECRET}&fields=fan_count
Using this in my web browser it works great and I get the fan count in json.
My problem is when I try to make this call using either cURL or file_get_contents inside a php script I dont seem to get anything back. After doing a var_dump I get bool(false) back. And after doing print_r I get nothing.
Is facebook somehow blocking my calls or what could be wrong? I have tried multiple ways I found online and none works. This is one of the examples:
<?php
$json_url = "https://graph.facebook.com/XXXXXXXXXXX?access_token=XXXXXXXXXXXXX&fields=fan_count";
$json = file_get_contents($json_url);
$json = json_decode($json, true);
print_r($json);
echo "Number of likes : ". $json[0]->fan_count;
?>
This is another:
<?php
$ch = curl_init("https://graph.facebook.com/v2.6/XXXXXXXXXXXXX?access_token=XXXXXXXXXXXX&fields=fan_count");
curl_setopt( $ch, CURLOPT_POST, false );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
curl_setopt( $ch, CURLOPT_HEADER, false );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$data = curl_exec( $ch );
echo $data;
?>
The problem seem to be right after the graph call. Can't figure out why, all help will be appreciated :) Thanks!

Related

Bypassing loading screen when getting HTML content with curl

We are using curl to get a response from a third-party webserver. there's a code snippet:
$url = "https://book.some-site.com/cgi-bin/booking-form.cgi";
$uagent = "Opera/9.80 (Windows NT 6.1; WOW64) Presto/2.12.388 Version/12.14";
$ch = curl_init( $url );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_setopt($ch, CURLOPT_USERAGENT, $uagent);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
Everything is working fine till we hit a loading screen on one of the pages. We get the following response from the webserver "...We are processing your request...Your search results will display shortly." which is a loading/waiting screen. after that we get nothing.
When working in a browser after the loading screen the actual response is displayed.
Any ideas how to get the actual response nad bypass the loading screen?
Thanks in advance.
Usually, when a website has a loading screen, then shows the results without redirecting you to a new page, it means they loaded the results via Ajax. So the HTML page loads with nothing but a "hey, it's loading" message, and then some JavaScript runs that downloads the actual content from a different page. You'll need to investigate their JS code and then load the page that they load via Ajax.
You might look into enabling "logging XMLHttpRequests" in your web browser's developer tools to make it easier to figure out what page they're loading via Ajax.

PHP curl returning blank strings

I started off using file_get_contents() and it returned string(9259) in which every character is a space (aka. its a lot of empty). After some research I tried using curl() and after a few struggles with getting the CURLOPT_SSL_VERIFYPEER and CURLOPT_USERAGENT to work it brought me right back to where I was, string(9259) of all blank.
I am attempting to retrieve the tracking information on multiple packages automatically and the code for a single iteration is as follows:
function curl($url)
{
$ch = curl_init();
curl_setopt( $ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17' );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
$data = curl_exec( $ch );
var_dump( curl_getinfo( $ch ) );
echo curl_errno( $ch ) . '<br/>';
echo curl_error( $ch ) . '<br/>';
curl_close( $ch );
return $data;
}
$url for one instance is https://www.fedex.com/fedextrack/?tracknumbers=055575670028673&cntry_code=us
My question is essentially why am I receiving the string(9259) of blank characters? I expected to receive an actual string representation of the website.
Maybe Fedex doesn't like it when you scrape pages, has detected that you are doing so, and is returning dummy data?
They do have APIs for this: http://www.fedex.com/us/developer/web-services/index.html

Why url returns JSON data via browser but shows nothing with curl (CURLOPT_RETURNTRANSFER is true)?

I want to get json data from here: JSON url;
Using Chrome I can see all JSON data, but using curl (below code) it seems to redirect and get lost (if CURLOPT_FOLLOWLOCATION is false, it does nothing):
$json_url = 'http://cartolafc.globo.com/mercado/filtrar.json?page=1&order_by=media&status_id=7&posicao_id=1';
$ch = curl_init($json_url);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
//curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, false );
//even killing the redirect process it does not return JSON data
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$data = curl_exec( $ch );
$dataDecoded = json_decode($data);
print_r($dataDecoded);
I saw something about simulating a browser with curl but I tought agent would do the trick. Maybe something about server using cookies... I really don't know. I saw other answers here today but they didn't solve my problem. Am I missing something?
Thank you.
Wrong variable use:
$data = curl_exec( $ch );
^^^^^--- data here
$dataDecoded = json_decode($json);
^^^^--- not $data here
So you're trying to decode a variable that doesn't exist.

how to post variables to file like jquery ajax but with php

I'm working on a project where I have to include a third party application in our system. This application transmitts parameters to a PHP file using jQuery ajax when calling the index.php. The exact function looks like this:
$.ajax( {
type : "POST",
cache : false,
crossDomain : false,
async : true,
url : "/somwhere/somefile.php",
data : "msg=37120&hello=true&USRID=1"
)};
I'm am not very experienced with AJAX at all but as far as I can see in the end the parameters "msg=37120&hello=true&USRID=1" get postet on somefile.php. Now I want to post the variables directly onto somefile.php using PHP instead of JS/AJAX.
After some researches I found the following solution to send post data directly to the somefile.php:
$url = "/somwhere/somefile.php";
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, "msg=37120&hello=true&USRID=1");
$response = curl_exec( $ch );
Unfortunately this is not working at all. No data arrives when I'm executing this code block. I've no idea what I'm doing wrong. Can someone tell me whats the mistake here?
Or is there even a better way how to do this?
Edit: Sorry -- I posted the get, below is the code to post.
This is the code that I use that works well:
$cookie="cookie.txt";
$useragent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)";
$data=array(
"field1" => 'data1',
"field2" => 'data2',
"SUBMIT" => 'true'
);
$cr = curl_init($url);
curl_setopt($cr, CURLOPT_RETURNTRANSFER, true); // Get returned value as string (dont put to screen)
curl_setopt($cr, CURLOPT_USERAGENT, $useragent); // Spoof the user-agent to be the browser that the user is on (and accessing the php $
curl_setopt($cr, CURLOPT_COOKIEJAR, $cookie); // Use cookie.txt for STORING cookies
curl_setopt($cr, CURLOPT_COOKIEFILE, $cookie); // Use cookie.txt for STORING cookies
curl_setopt($cr, CURLOPT_POST, true); // Tell curl that we are posting data
curl_setopt($cr, CURLOPT_POSTFIELDS, $data); // Post the data in the array above
$output = curl_exec($cr);

PHP CURL - Problems storing and using cookies when scraping

I've been trying to write a script that retrieves Google trends results for a given keyword. Please note im not trying to do anything malicious I just want to be able to automate this process and run it a few times every day.
After investigating the Google trends page I discovered that the information is available using the following URL:
http://www.google.com/trends/trendsReport?hl=en-GB&q=keyword&cmpt=q&content=1
You can request that information mutliple times with no issues from a browser, but if you try with "privacy mode" after 4 or 5 requests the following is displayed:
An error has been detected You have reached your quota limit. Please
try again later.
This makes me think that cookies are required. So I have written my script as follows:
$cookiefile = $siteurl . '/wp-content/plugins/' . basename(dirname(__FILE__)) . '/cookies.txt';
$url = 'http://www.google.com/trends/trendsReport?hl=en-GB&q=keyword&cmpt=q&content=1';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$x='error';
while (trim($x) != '' ){
$html=curl_exec($ch);
$x=curl_error($ch);
}
echo "test cookiefile contents = ".file_get_contents($cookiefile)."<br />";
echo $html;
However I just can't get anything written to my cookies file. So I keep on getting the error message. Can anyone see where I'm going wrong with this?
I'm pretty sure your cookie file should exist before you can use it with curl.
Try:
$h = fopen($cookiefile, "x+");

Categories