I am using Facebook's graph API in PHP to access a user's feed.
Specifically, I want to record the names of people who posted and people who were tagged in the post.
I have been able to retrieve the name of people who posted by using the methods:
getProperty('from')->getProperty('name')
But, for whatever reason, I have not been successful in getting who the was tagged (despite similarly using getProperty('to') ).
As well, I have been able to accomplish this in javascript (so I believe I am correctly interpreting the structure of the data), but not PHP.
If anyone can help me retrieve the name of who is tagged in the feed by accessing the property with the getProperty methods that I have been using or by accessing the name property another way, that would be greatly appreciated.
Below is my best attempt at coding this section:
/* make the API call to get home*/
$request = new FacebookRequest(
$session,
'GET',
'/me/home'
);
$response = $request->execute();
$graphObject = $response->getGraphObject();
//array of the 25 most recent posts
$dataPostArray = $graphObject->getPropertyAsArray('data');
$i = 0;
while($dataPostArray[$i])
{
//print_r($r->getProperty('name'));
if($dataPostArray[$i]->getProperty('from') )
{
echo $dataPostArray[$i]->getProperty('from')->getProperty('name');//works as intended
}
if($dataPostArray[$i]->getProperty('to') )
{
$temp = $dataPostArray[$i]->getProperty('to');
$dataToArray = $temp->getPropertyAsArray('data');
echo $dataToArray[0]->getProperty('name');
}
$i++;
}
I think your issue may be that the getPropertyAsArray method is not an option for when you are getting the data property. As an alternative to the getProperty methods. You could do it like such:
$graphObject = $response->getGraphObject()->asArray();
$i = 0;
while($graphObject['data'][$i])
{
if($graphObject['data'][$i]->from )
{
//echo $graphObject['data'][$i]->from->name;//will work just as you have above
}
$j = 0;
if($graphObject['data'][$i]->to->data[$j]->name )
{
echo '<pre>' . print_r( $graphObject['data'][$i]->to->data[$j]->name, 1 ) . '</pre>';
}
$i++;
}
}
Related
I am currently working with the Amazon MWS to integrate some features into wordpress via a plugin. I am using the client libraries provided by amazon found here:
https://developer.amazonservices.com/api.html?group=bde§ion=reports&version=latest
Using these client libraries and the sample php files included I have set up my plugin to make two API calls. The first is requestReport
public function requestInventoryReport() {
AWI_Amazon_Config::defineCredentials(); // Defines data for API Call
$serviceUrl = "https://mws.amazonservices.com";
$config = array (
'ServiceURL' => $serviceUrl,
'ProxyHost' => null,
'ProxyPort' => -1,
'MaxErrorRetry' => 3,
);
$service = new MarketplaceWebService_Client(
AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY,
$config,
APPLICATION_NAME,
APPLICATION_VERSION);
$request = new MarketplaceWebService_Model_RequestReportRequest();
$request->setMerchant(MERCHANT_ID);
$request->setReportType('_GET_MERCHANT_LISTINGS_DATA_');
self::invokeRequestReport($service, $request);
}
private function invokeRequestReport(MarketplaceWebService_Interface $service, $request) {
try {
$response = $service->requestReport($request);
if ($response->isSetRequestReportResult()) {
// Print Out Data
}
} catch (MarketplaceWebService_Exception $ex) {
// Print Out Error
}
}
and the second is getReportRequestList which has code similar to the first function. I am able to run these functions without any errors. The issue that I am having is that $response->isSetRequestReportResult() returns false. From my understanding and looking into the response object, this would suggest that the response object does not have the result. (Upon printing out the response object I can see that the FieldValue of the result array is NULL.) The call, however, does not throw an error but neither does it have the result.
I did some digging through the code and found that the result does actually get returned from the api call but never gets set to the return object when the library attempts to parse it from XML. I've tracked the error down to this block of code (This code is untouched by me and directly from the amazon mws reports library).
private function fromDOMElement(DOMElement $dom)
{
$xpath = new DOMXPath($dom->ownerDocument);
$xpath->registerNamespace('a', 'http://mws.amazonaws.com/doc/2009-01-01/');
foreach ($this->fields as $fieldName => $field) {
$fieldType = $field['FieldType'];
if (is_array($fieldType)) {
if ($this->isComplexType($fieldType[0])) {
// Handle Data
} else {
// Handle Data
}
} else {
if ($this->isComplexType($fieldType)) {
// Handle Data
} else {
$element = $xpath->query("./a:$fieldName/text()", $dom);
$data = null;
if ($element->length == 1) {
switch($this->fields[$fieldName]['FieldType']) {
case 'DateTime':
$data = new DateTime($element->item(0)->data,
new DateTimeZone('UTC'));
break;
case 'bool':
$value = $element->item(0)->data;
$data = $value === 'true' ? true : false;
break;
default:
$data = $element->item(0)->data;
break;
}
$this->fields[$fieldName]['FieldValue'] = $data;
}
}
}
}
}
The data that should go into the RequestReportResult exists at the beginning of this function as a node in the dom element. The flow of logic takes it into the last else statement inside the foreach. The code runs its query and returns $element however $element->length = 13 in my case which causes it to fail the if statement and never set the data to the object. I have also looked into $element->item(0) to see what was in it and it appears to be a dom object itself matching the original dom object but with a bunch of empty strings.
Now, I'm new to working with the MWS and my gut feeling is that I am missing a parameter somewhere in my api call that is messing up how the data is returned and is causing this weird error, but I'm out of ideas at this point. If anyone has any ideas or could point me in the right direction, I would greatly appreciate it.
Thanks for your time!
** Also as a side note, Amazon Scratchpad does return everything properly using the same parameters that I am using in my code **
These works for me, check if you are missing anything.
For RequestReportRequest i am doing this:
$request = new MarketplaceWebService_Model_RequestReportRequest();
$marketplaceIdArray = array("Id" => array($pos_data['marketplace_id']));
$request->setMarketplaceIdList($marketplaceIdArray);
$request->setMerchant($pos_data['merchant_id']);
$request->setReportType($this->report_type);
For GetReportRequestList i am doing this:
$service = new MarketplaceWebService_Client($pos_data['aws_access_key'], $pos_data['aws_secret_access_key'], $pos_data['config'], $pos_data['application_name'], $pos_data['application_version']);
$report_request = new MarketplaceWebService_Model_GetReportRequestListRequest();
$report_request->setMerchant($pos_data["merchant_id"]);
$report_type_request = new MarketplaceWebService_Model_TypeList();
$report_type_request->setType($this->report_type);
$report_request->setReportTypeList($report_type_request);
$report_request_status = $this->invokeGetReportRequestList($service, $report_request, $report_requestID);
i'm developing a web app (with PHP SDK) that basically take user posts of last year and do some kind of analysis on it.
Untill now i used to load the data with a recursive function that everytime get the content from the paging link and call itself again with that data.
i've just discovered that i can use getRequestForNextPage(); so im tring to use this function but i got some troubles , seems that getRequestForNextPage() do not respect request's parameters. I know my explanation is pretty confused, here a simplified version of my code:
$thedate= strtotime('now ,-1 year');
$today = strtotime('now');
$richiesta ="/me/posts?since=".$thedate."&until=".$today;
$request = new FacebookRequest( // richiedo i miei statuses
$session,
'GET',
$richiesta
);
$respons = $request->execute();
function looper($response){
$graphObject = $response->getGraphObject();
$nextPageRequest = $response->getRequestForNextPage();
$respy = $nextPageRequest->execute();
$x = $graphObject->getProperty('data');
$y = $x->asArray();
$counter = 0 ;
foreach ($y as $el){
//do something
}
if ( $respy != false ){
looper($respy);
} else {
return;
}
}
looper($respons);`
it seems that that function just keep to iterate even if the posts are older than 1 year.
do getRequestForNextPage() just dont care about request parameters? it just check if there is a "next" paging link in the graph object? Thanks in advance for any suggestion
I am using a few of the Facebook Graph API methods that have pagination successfully using cursor-based pagination, similar to this:
echo '<ul>';
$params = array('limit' => 10);
do {
$groups = (new FacebookRequest(
$session, 'GET', '/me/groups', $params
))->execute()->getGraphObject();
if (null !== $groups->getProperty('paging') && null != $groups->getProperty('paging')->getProperty('next')) {
$params = array('limit' => 10, 'after' => $groups->getProperty('paging')->getProperty('cursors')->getProperty('after'));
} else {
$params = null;
}
foreach ($groups->getProperty('data')->asArray() as $group) {
echo '<li>' . $group->name . '</li>';
}
} while ($params !== null);
echo '</ul>';
This simple code will grab all the groups of the current user. It checks that the paging and paging/next properties are present and if so uses the cursor to setup another iteration of the loop. I realise now this could probably have been done better as the cursor isn't always available. When I use the /{group-id}/feed API endpoint there are the previous and next links but no cursor.
So, how am I supposed to make paginated requests when there is no cursor with the Facebook PHP SDK?
I see other answers suggesting using cURL or even file_get_contents to grab the next and previous URLs but that seems very silly considering I'm using the PHP SDK here - surely there's a built-in way?
I'm using facebook/php-sdk-v4 with Composer - there doesn't seem to be the (old?) $facebook->api(...) functionality availble here either.
Have a look at
https://developers.facebook.com/docs/php/FacebookResponse/4.0.0
There is a method getRequestForNextPage() in the PHP SDK v4.0.0.
// A FacebookResponse is returned from an executed FacebookRequest
try {
$response = (new FacebookRequest($session, 'GET', '/me'))->execute();
// You can get the request back:
$request = $response->getRequest();
// You can get the response as a GraphObject:
$object = $response->getGraphObject();
// You can get the response as a subclass of GraphObject:
$me = $response->getGraphObject(GraphUser::className());
// If this response has multiple pages, you can get a request for the next or previous pages:
$nextPageRequest = $response->getRequestForNextPage();
$previousPageRequest = $response->getRequestForPreviousPage();
} catch (FacebookRequestException $ex) {
echo $ex->getMessage();
} catch (\Exception $ex) {
echo $ex->getMessage();
}
By looking at the source code at
https://github.com/facebook/facebook-php-sdk-v4/blob/4.0-dev/src/Facebook/FacebookResponse.php#L164
it just handles the next property:
return $this->handlePagination('next');
IMHO, using the next property as a default should be fine, opposed to cursors. Furthermore, I don't even see a cursors property when querying a sample group's feed, so this might be obsolete.
References:
https://developers.facebook.com/docs/graph-api/using-graph-api/v2.3#paging
I have a website that contains a form that makes various SOAP requests at certain points. One of these requests gets a list of induction times returned and displays them to the user in order for them to pick one.
I am getting results returned fine from the SOAP service but unfortunately it seems to be not showing information correctly and even not displaying returned object keys at all.
I have liased with one of the devs at the SOAP end and he says the service is fine and spitting out the cirrect information. He has provided a screentshot:
Here is my code to pull call the method I need for this information:
public function getInductionTimes($options) {
$client = $this->createSoapRequest();
$inductionTimes = $client->FITinductionlist($options);
//die(print_r($inductionTimes));
return $inductionTimes;
}
private function createSoapRequest() {
$url = 'https://fitspace.m-cloudapps.com:444/FITSPACE/MHservice.asmx?WSDL';
$options["connection_timeout"] = 25;
$options["location"] = $url;
$options['trace'] = 1;
$options['style'] = SOAP_RPC;
$options['use'] = SOAP_ENCODED;
$client = new SoapClient($url, $options);
//die(print_R($client->__getFunctions()));
return $client;
}
As you can see I print_r the code right after I have received it to check what I am getting returned and it is this:
As you can see this IDdtstring field is getting completely ignored.
Does anyone have any ideas as to why this may be happening? Is it something to do with encoding? I can't seem to get anywhere on this issue!
Thanks
I was able to retrieve the fields correctly, including IDdtstring, using your basic code. Perhaps you are not sending the parameters correctly?
function getInductionTimes($options) {
$client = createSoapRequest();
$inductionTimes = $client->FITinductionlist($options);
die(print_r($inductionTimes));
return $inductionTimes;
}
function createSoapRequest() {
$url = 'https://fitspace.m-cloudapps.com:444/FITSPACE/MHservice.asmx?WSDL';
$options["connection_timeout"] = 25;
$options["location"] = $url;
$options['trace'] = 1;
$options['style'] = SOAP_RPC;
$options['use'] = SOAP_ENCODED;
$client = new SoapClient($url, $options);
//die(print_R($client->__getFunctions()));
return $client;
}
getInductionTimes(array("IDDate" => "2013-06-28T13:00:00+01:00", "GYMNAME" => "Bournemouth"));
I managed to solve this issue by adding the line of code into my SOAP options array which I then presume was an issue with my WSDL being cached in PHP:
$options['cache_wsdl'] = WSDL_CACHE_NONE;
I've just started working with the PHP API for Rackspace Cloud Files. So far so good-- but I am using it as sort of a poor man's memcache, storing key/value pairs of serialized data.
My app attempts to grab the existing cached object by its key ('name' in the API language) using something like this:
$obj = $this->container->get_object($key);
The problem is, if the object doesn't exist, the API throws a fatal error rather than simply returning false. The "right" way to do this by the API would probably be to do a
$objs = $this->container->list_objects();
and then check for my $key value in that list. However, this seems way more time/CPU intensive than just returning false from the get_object request.
Is there a way to do a "search for object" or "check if object exists" in Cloud Files?
Thanks
I sent them a pull request and hope it'll get included.
https://github.com/rackspace/php-cloudfiles/pull/35
My pull-request includes an example, for you it would be similar to this:
$object = new CF_Object($this->container, 'key');
if ($object->exists() === false) {
echo "The object '{$object->name}' does not exist.";
}
I have more general way to check if object exists:
try {
$this->_container->get_object($path);
$booExists = true;
} catch (Exception $e) {
$booExists = false;
}
If you dump the $object, you'll see that content_length is zero. Or, last modified will be a zero length string.
Example:
$object = new CF_Object($container, 'thisdocaintthere.pdf');
print_r($object->content_length);
There is also, deep in the dumped parent object, a 404 that will return, but it's private, so you'd need to some hackin' to get at it.
To see this, do the following:
$object = new CF_Object($container, 'thisdocaintthere.pdf');
print_r($object->container->cfs_http);
You'll see inside that object a response_status that is 404
[response_status:CF_Http:private] => 404
I know I'm a little late to the party, but hopefully this will help someone in the future: you can use the objectExists() method to test if an object is available.
public static function getObject($container, $filename, $expirationTime = false)
{
if ($container->objectExists($filename)) {
$object = $container->getPartialObject($filename);
// return a private, temporary url
if ($expirationTime) {
return $object->getTemporaryUrl($expirationTime, 'GET');
}
// return a public url
return $object->getPublicUrl();
}
// object does not exist
return '';
}
Use like...
// public CDN file
$photo = self::getObject($container, 'myPublicfile.jpg');
// private file; temporary link expires after 60 seconds
$photo = self::getObject($container, 'myPrivatefile.jpg', 60);
If you do not want to import opencloud to perform this check you can use the following:
$url = 'YOUR CDN URL';
$code = FALSE;
$options['http'] = array(
'method' => "HEAD",
'ignore_errors' => 1,
'max_redirects' => 0
);
$body = file_get_contents($url, NULL, stream_context_create($options));
sscanf($http_response_header[0], 'HTTP/%*d.%*d %d', $code);
if($code!='200') {
echo 'failed';
} else {
echo 'exists';
}