I am trying to post Facebook Page status as page(not user) through my script. This returns me an error 100.
Here I am generating temporary user_access_token from Graph Explorer with permission : manage_pages, publish_pages
Code:
<?php
$url='https://graph.facebook.com/v2.3/{$user_id}/accounts?access_token=USER_ACCESS_TOKEN';
$ch=curl_init();
CURL_SETOPT($ch,CURLOPT_URL,$url);
CURL_SETOPT($ch,CURLOPT_RETURNTRANSFER, 1);
$json=json_decode(curl_exec($ch));
$page_access_token=$json->data['0']->access_token;
curl_close($ch);
$page_id='xxx';
$message='helloworld';
$url="https://graph.facebook.com/v2.3/{$page_id}?access_token=$page_access_token";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $message);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
$result = json_decode(curl_exec($curl));
var_dump($result);
?>
If everything goes well, a string "helloworld" should get posted on Facebook page. But here it is returning with an error :
object(stdClass)#5 (1) {
["error"]=>
object(stdClass)#6 (3) {
["message"]=>
string(61) "(#100) Parameters do not match any fields that can be updated"
["type"]=>
string(14) "OAuthException"
["code"]=>
int(100)
}
}
What is mistake here ? Thank you.
You're trying to post to /<PAGE_ID>
The correct endpoint for creating a post on a Page is /<PAGE_ID>/feed, documented here: https://developers.facebook.com/docs/graph-api/reference/v2.3/page/feed
A valid format for a basic call to create a post would be https://graph.facebook.com/v2.3/<PAGE_ID>/feed?message=helloworld&access_token=<ACCESS_TOKEN>
Related
I´m trying to establish a connection to the Klarna playground server. For this case I need to create a HPP Session with the session_id from a customer. I already get the session_id with cURL, but when I try to send another cURL request, the content type is NULL.
I tried to create a function to set the cURL options properly, which works fine to create a KP session to receive the session_id from Klarna, but not for the HPP Session.
This code is to set the options for the cURL session, since just the URL and the data is changing, I provide them as a parameter.
private function executeCurl( $url, $data ) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->setHeader()); // set header for request
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); // set authorization
curl_setopt($ch, CURLOPT_USERPWD, $this->username() . ":" . $this->password()); // set authorization
curl_setopt($ch, CURLOPT_URL, $url); // set URL
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // set data for request
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curl = curl_exec($ch);
$info = curl_getinfo($ch);
var_dump($info);
curl_close($ch);
return $curl;
}
In this function the KP session_id is properly created and calls the function hostpayment.
public function start() {
$data = $this->createJson();
$curl = $this->executeCurl("https://api.playground.klarna.com/payments/v1/sessions", $data);
$data = json_decode($curl,true);
$session_id = $data["session_id"];
$this->hostpayment($session_id);
}
In this function the HPP session should be created.
public function hostpayment($session_id) {
$url = "https://api.playground.klarna.com/payments/v1/sessions/" . $session_id;
$data = $this->createSessionJson($session_id);
$curl = $this->executeCurl($url, $data);
$data = json_decode($curl,true);
}
I would expect, that the cURL output in both cases are the same, except for the URL and the data.
curl_getinfo outputed the following for the KP Session:
{ ["url"]=> string(54) "https://api.playground.klarna.com/payments/v1/sessions" ["content_type"]=> string(16) "application/json" ["http_code"]=> int(200) ["header_size"]=> int(290) ["request_size"]=> int(271)}
The HPP request outputs the following:
{ ["url"]=> string(91) "https://api.playground.klarna.com/payments/v1/sessions/53ac5196-ced3-7573-96c7-332a7b8ab0ae" ["content_type"]=> NULL ["http_code"]=> int(204) ["header_size"]=> int(212) ["request_size"]=> int(307)}
You can see that the content type in the second request is null and the HTTP code is 204, so the request is successful, but the header isn´t set properly.
As I was trying to find out the problem, I was realising after hours of debugging, that Klarna uses very similar URLs, so instead of https://api.playground.klarna.com/payments/v1/sessions/ I had to use https://api.playground.klarna.com/hpp/v1/sessions/ to receive the right data.
I tried integrating Azure ML API with PHP but unfortunately getting an error in response.
Updated: I have used request response API sending through json response
Below is the response obtained on executing PHP script:
array(1) { ["error"]=> array(3) { ["code"]=> string(11) "BadArgument"
["message"]=> string(26) "Invalid argument provided." ["details"]=> array(1)
{[0]=> array(2) { ["code"]=> string(18) "RequestBodyInvalid" ["message"]=>
string(68) "No request body provided or error in deserializing the request
body." } } } }
PHP Script:
$url = 'URL';
$api_key = 'API';
$data = array(
'Inputs'=> array(
'My Experiment Name'=> array(
"ColumnNames" => [['Column1'],
['Column2'],
['Column3'],
['Column4'],
['Column5'],
['Column6'],
['Column7']],
"Values" => [ ['Value1'],
['Value2'],
['Value3'],
['Value4'],
['Value5'],
['Value6'],
['Value7']]
),
),
'GlobalParameters' => new StdClass(),
);
$body = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer '.$api_key, 'Accept: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = json_decode(curl_exec($ch), true);
//echo 'Curl error: ' . curl_error($ch);
curl_close($ch);
var_dump ($response);
I have followed few examples, still unable to crack it. Please let me know the solution for this.
According to the error information, I think the issue was caused by requesting the ML REST API without correct json body.
I suggest that you can refer to the article "Getting started with the Text Analytics APIs to detect sentiment, key phrases, topics and language" to correctly format your input rows in JSON as the request body and try again.
Hope it helps.
If you can update your question for specifying which ML REST API you used, I think it's very helpful for figuring out the issue.
Expect your update.
I'm trying to parse a JSON answer from Redmine API and I don't know how to get to the parts of the array.
Here is the code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://login:password#redmine.server/redmine/issues.json?cf_2=12345');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$data = json_decode($response);
When I make a var_dump($data), the answer looks like this:
array(1) { [0]=> object(stdClass)#1853 (14) { ["id"]=> int(96) ["project"]=> object(stdClass)#1852 (2) { ["id"]=> int(68) ["name"]=> string(7) "Test.......
So, when I make a for loop, I would like to access the parts of the array:
foreach($data as $issues){
var_dump($issues["id"]);
}
And so on. Any idea on this?
Stupid me...
The culprit was here:
$data = json_decode($response);
Should be:
$data = json_decode($response,true);
Now I get a proper PHP array.
I am trying to get title of the webpage, see the code below..
include("simple_html_dom.php");
$url = "http://fzmoviez.in/video/list/4059539";
$base = $url;
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_URL, $base);
curl_setopt($curl, CURLOPT_REFERER, $base);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$str = curl_exec($curl);
curl_close($curl);
// Create a DOM object
$html_base = new simple_html_dom();
// Load HTML from a string
$html_base->load($str);
$title = $html_base->getElementsByTagName("title")->innertext; //line 21
echo $title;
I am getting error:
Notice: Trying to get property of non-object in C:\xampp\htdocs\... on line 21
but when I var_dump($html_base) I get
object(simple_html_dom)#1 (23) { ["root"]=> object(simple...
... card" ["attr"]=> array(2) { ["id"]=> string(5) "index" ["title"]=> string(179) "FzMoviez.iN - Free Download Latest Bollywood Movies,Hindi Dudded Movies,Hollywood Movies,Live Movies,WWE Shows,Mp4,HD Mp4,High Quality Mp4,Avi,Iphone,Android,Tablets And Many More" } ["children"]=> array(25) { [0]=> object(simple_html_dom_node)#55 (9) { ["nodetype"]=>......
meaning, it is an object, and title is there, why giving error
Notice: Trying to get property of non-object in C:\xampp\htdocs\.. on line 21
$html_base->getElementsByTagName("title")
Will return array of elements, you need to iterate through this array to get your data.
So i have a small PHP script to generate short links, it work but some times i got this error :
Undefined property: stdClass::$id","file":ShortLink.php","line":31
This is my script :
<?php
class ShortLink {
public static function generateShortLink($longUrl)
{
//This is the URL you want to shorten
$apiKey = 'MY_API_KEY';
//Get API key from : http://code.google.com/apis/console/
$postData = array('longUrl' => $longUrl, 'key' => $apiKey);
$jsonData = json_encode($postData);
$curlObj = curl_init();
curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url');
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curlObj, CURLOPT_HEADER, 0);
curl_setopt($curlObj, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
curl_setopt($curlObj, CURLOPT_POST, 1);
curl_setopt($curlObj, CURLOPT_POSTFIELDS, $jsonData);
$response = curl_exec($curlObj);
//change the response json string to object
$json = json_decode($response);
curl_close($curlObj);
return $json->id;
}
}
When i start worked with this script 6 or 7 months ago i hadn't this error but now i start get it and i have no idea why, so please if someone has any idea i will be very appreciative.
Update :
When i vardump my $json i get that :
{ ["domain"]=> string(11) "usageLimits" ["reason"]=> string(26) "userRateLimitExceededUnreg" ["message"]=> string(40) "User Rate Limit Exceeded. Please sign up" ["extendedHelp"]=> string(36) "https://code.google.com/apis/console" } } ["code"]=> int(403) ["message"]=> string(40) "User Rate Limit Exceeded. Please sign up" }}
So i wondered if Google limited the Google shorten service ?
class ShortLink {
public static function generateShortLink($longUrl)
{
//This is the URL you want to shorten
$apiKey = 'YOUR_SERVER_API_KEY';
//Get API key from : http://code.google.com/apis/console/
$postData = array('longUrl' => $longUrl, 'key' => $apiKey);
$jsonData = json_encode($postData);
$curlObj = curl_init();
curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url');
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curlObj, CURLOPT_HEADER, 0);
curl_setopt($curlObj, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
curl_setopt($curlObj, CURLOPT_POST, 1);
curl_setopt($curlObj, CURLOPT_POSTFIELDS, $jsonData);
$response = curl_exec($curlObj);
//change the response json string to object
$json = json_decode($response);
curl_close($curlObj);
if(!is_object($json))
{
return(false);
}
return $json->id;
}
}
$api = new ShortLink();
$shorturlid=$api->generateShortLink('http://avecsrthgdgnb.avcd');
echo $shorturlid;
are you using new console then enable URL Shortener API.
Sometimes because of network curl is not returned with response within 30 seconds (default time limit in php for a cmd to finish)
Try changing the time limit in php.ini or if that is not in your control or you do not want to modify it for all the php cmds try bool set_time_limit ( int $seconds ) before calling curl_exec
Update:
I see there is no id field in the json that is returned.
{ ["domain"]=> string(11) "usageLimits"
["reason"]=> string(26) "userRateLimitExceededUnreg"
["message"]=> string(40) "User Rate Limit Exceeded. Please sign up"
["extendedHelp"]=> string(36) "https://code.google.com/apis/console"
}
}
["code"]=> int(403)
["message"]=> string(40) "User Rate Limit Exceeded. Please sign up"
}
}
And if you closely see your User rate limit has been reached See here for details on limitation on using google apis. (You may try different IP i.e. different machine or different api key and same code may start working).
Hope this helps