Getting 404 Error from Facebook Graph API - facebook

So I am getting this error and I have no clue why. When I run it on the server it's on, I get a 404 error with a simple request like this.
$json = file_get_contents('https://graph.facebook.com/me?access_token='.LONGSTRING);
The error is:
function.file-get-contents: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
However, I can paste the URL that I am using with file_get_contents directly in the browser at the same time and it comes up. So it seems as if Facebook is blocking my server.
Also it works half the time.
Any ideas?

Try cURL, not sure why file_get_contents is unreliable but I have seen the same issue in the past. I use this geturl function to cURL a URL and pass the parameters in as a PHP array
function geturl($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;
}
Which can then be called like so
$url = 'https://www.graph.facebook.com/me';
$params = array('access_token' => '*********************');
$graph_ret = geturl($url, $params);

$json = #file_get_contents('https://graph.facebook.com/me?access_token='.$access_token) or die("ERROR");
use this will help you alot

Related

Upload file to JIRA REST API with PHP cUrl

I want upload a file to JIRA with REST API.
My code :
$ch_api = curl_init('https://website.com/rest/api/2/issue/SI-10255/attachments');
curl_setopt($ch_api, CURLOPT_POST, true);
curl_setopt($ch_api, CURLOPT_POSTFIELDS, json_encode(["file" => "#t.txt"]));
curl_setopt($ch_api, CURLOPT_HTTPHEADER, array('Content-Type:multipart/form-data','X-Atlassian-Token:no-check'));
curl_setopt($ch, CURLOPT_USERPWD, "login:password");
curl_setopt($ch_api, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch_api);
curl_close($ch_api);
And i have this error :
the request was rejected because no multipart boundary was found
And when i add a boundary, the file is not uploaded. The response from api was a blank array.
Please help.
Thanks.
For upload file to JIRA REST API, not use the header "Content-Type:multipart/form-data"
And for the file, use this code :
if (function_exists('curl_file_create')) {
$cFile = curl_file_create("file.txt");
} else {
$cFile = '#' . realpath("file.txt");
}

facebook realtime subsciption API error

I'm using this link in order to add new subsciption entry:
https://graph.facebook.com/XXX/subscriptions?access_token=YYY&object=payments&callback_url=http://xxx/rlcallback.php&fields=actions,disputes&verify_token=ZZZ
For some reason, I get error:
{
"error": {
"message": "(#100) Invalid object. object should be url or open graph object id.",
"type": "OAuthException",
"code": 100
}
}
But the object "payments" inside my link is clearly valid. What am I missing here?
Make sure you are using the correct parameters: object, callback_url, fields, verify_token...and of course the access_token.
Also (and that may be the problem in this case), you have to use POST, not GET. You can either use CURL with POST to subscribe to the Realtime API, or you just use one of the SDKs as explained in the Facebook docs: https://developers.facebook.com/docs/graph-api/reference/v2.1/app/subscriptions
Here is one example with CURL:
$appsecretProof = hash_hmac('sha256', FBAPPID . '|' . FBSECRET, FBSECRET);
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'facebook-php');
$postData = 'object=page' .
'&callback_url=' . urlencode('http://yourdomain.com/callback.php') .
'&fields=feed' .
'&verify_token=somethingfancy' .
'&access_token=' . FBAPPID . '|' . FBSECRET .
'&appsecret_proof=' . $appsecretProof;
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/' . FBAPPID . '/subscriptions');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$curlResult = curl_exec($ch);
you need to specify object, callback_url, fields and verify_token parameters as your curl post parameters
example:
curl -F 'object=user' \
-F 'callback_url=' \
-F 'fields=checkins' \
-F 'verify_token=' \
"https://graph.facebook.com//subscriptions?access_token=

Convert Php Curl Function to Perl

It would be great if someone could help convert this function from Php to Perl, basically this function is meant to get contents of a https url. Right now I'm not good with Perl, I haven't learnt on how to use cURL just yet.
function curl_get_content($url)
{
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($handle, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($handle, CURLOPT_SSLVERSION, 3);
$data = curl_exec($handle);
curl_close($handle);
return $data;
}
Thanks!
EDIT:
I attempted to use this
my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 1 });
my $header = HTTP::Request->new(GET => $URL);
my $request = HTTP::Request->new('GET', $URL, $header);
my $response = $ua->request($request);
if ($response->is_success){
print "URL:$URL\nHeaders:\n";
print $response->headers_as_string;
}elsif ($response->is_error){
print "Error:$URL\n";
print $response->error_as_HTML;
}
But it keeps giving me server error: 500.
My suspicion is that you're not setting the headers correctly so when the server is receiving your request and trying to process the headers, it's not understanding the content and just giving up (giving you the 500 error.) In your code, you're setting $header to a HTTP::Request object but that should actually be a HTTP::Headers object. Set that to:
my $header = HTTP::Headers->new();
$header->header( 'Content-Type' => 'text/plain' ); # set content-type to what you need
# you can add additional headers if needed

Intuit QBMS Error: Unknown error returned by SAX parser

I'm trying to set up a credit card interface on a website using Intuit's QBMS and I've got the XML request set up per the specifications on the Intuit documentation. I've also got my AppID, Connection Ticket, and Application Login set up. But when I do a test, I'm getting the following error:
Unknown error returned by SAX parser. Exception from other package: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
Here is the XML Request:
$productionUrl = 'https://merchantaccount.quickbooks.com/j/AppGateway';
$connectionTicket = 'SDK-xxx-xxx-xxxxxxxxxxxxxxxxxxxxxxxx';
$applicationLogin = 'qbms.site.com';
$appID = "111111111";
$xml = rawurlencode('<?xml version="1.0"?>
<?qbmsxml version="4.1"?>
<QBMSXML>
<SignonMsgsRq>
<SignonDesktopRq>
<ClientDateTime>' . date('Y-m-d\TH:i:s') . '</ClientDateTime>
<ApplicationLogin>' . $applicationLogin . '</ApplicationLogin>
<ConnectionTicket>' . $connectionTicket . '</ConnectionTicket>
<Language>English</Language>
<AppID>' . $appID . '</AppID>
<AppVer>1.0</AppVer>
</SignonDesktopRq>
</SignonMsgsRq>
<QBMSXMLMsgsRq>
<CustomerCreditCardChargeRq>
<TransRequestID>' . $_transId . '</TransRequestID>
<CreditCardNumber>' . $_cc_number . '</CreditCardNumber>
<ExpirationMonth>' . $_cc_exp_month . '</ExpirationMonth>
<ExpirationYear>' . $_cc_exp_year . '</ExpirationYear>
<IsCardPresent>false</IsCardPresent>
<Amount>' . $_amount . '</Amount>
<NameOnCard>' . $_cc_name . '</NameOnCard>
<CreditCardAddress>' . $_cc_address . '</CreditCardAddress>
<CreditCardPostalCode>' . $_cc_zip . '</CreditCardPostalCode>
<SalesTaxAmount>' . $_tax . '</SalesTaxAmount>
</CustomerCreditCardChargeRq>
</QBMSXMLMsgsRq>
</QBMSXML>');
$header[] = "Content-type: application/x-qbmsxml";
$header[] = "Content-length: " . strlen($xml);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_URL, $productionUrl);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
$response = curl_exec($ch);
curl_close($ch);
print_r($response);
Since the documentation is still not available, in particular https://developer.intuit.com/qbsdk-current/Common/newOSR/index.html
This is speculation based on my testing. Check to see that the <TransRequestID>
contains a number, with only leading or following alpha characters (if any).
The response contains this xml: <TxnAuthorizationStamp>
which is your TransRequestID incremented by 1. I think if it can't figure out how to increment it will fail.
Please update any progress you have.
I know that this is an old question but I ran into similar problems and was able to get it working.
When I removed this phrase from the request xml packet:
<?xml version="1.0"?>
The SAX parser error went away and it works.

How to test Google+ and Facebook buttons?

I added custom Google+ and Facebook buttons, and counter is updated over MY ajax...
Here is the code ( I included for twitter counter too so it can be useful for others, but it's counter is working for sure ):
$url = "URL OF PAGE";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://clients6.google.com/rpc?key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"' . $url . '","source":"widget","userId":"#viewer","groupId":"#self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
$curl_results = curl_exec ($ch);
curl_close ($ch);
$parsed_results = json_decode($curl_results, true);
echo $parsed_results[0]['result']['metadata']['globalCounts']['count']; //Google+ counter for this URL
$fbresponse = file_get_contents('http://graph.facebook.com/' .$url);
$fbresponse_c = json_decode($fbresponse,true);
if ( $fbresponse_c['shares'] ) { echo $fbresponse_c['shares']; } else { echo '0'; } //Facebook counter for this URL
$twresponse = file_get_contents('http://cdn.api.twitter.com/1/urls/count.json?url=' .$url. '&callback=twttr.receiveCount');
$twresponse = str_replace('twttr.receiveCount(' ,'', $twresponse);
$twresponse = str_replace(')' ,'', $twresponse);
$twresponse_c = json_decode($twresponse,true);
echo $twresponse_c['count']; //Twitter counter for this URL
I changed URL to "http://www.google.com" and it shows counters from all sites 3 sites...
I shared link on my Facebook Wall, using my pop-up ( code is below ) but counter didn't updated ( it didn't updated for two days )...
And for Google+ I don't have account so I can't test it...
Can someone tell me way how to test those?
Since from what I see it's not working...
For sharing I pop-up this URL for Facebook:
"http://www.facebook.com/sharer.php?u=" + escape(url) + "&t=Some text"
and this one for Google:
"https://plusone.google.com/u/0/+1/profile/?type=po&source=p&parent=https%3A%2F%2Fplusone.google.com&hl=en_US&ru=" + escape(urll)
How often facebook updates it's counters?
And how can I check is this Google+ counter working okay?
Thanks!
Everything is updating now...
Facebook instantly, and I opened Google+ account so I tested that too, it's also working...
-Closed-