Bing Image Search API results exipres after some time - php

I am implementing Bing Image Search API in php. I know that Bing API has been changed and now we have to involve that windows azure marketplace thing in-order to use the Bing Image Search API.
I have done that, which means i have opted for a free Bing Search Api subscription which gives me around 5000 transaction per month. Its going all good but the thing is the result which is being fetched is tend to get expired after say 1 month.
Here is the code i am using :
$key = "cricket";
// Replace this value with your account key
$accountKey = 'WEGUEed3yF9CI6ZzVblKD0HoMRG3/rOELkCda9VYsuk=';
$ServiceRootURL = 'https://api.datamarket.azure.com/Bing/Search/';
$WebSearchURL = $ServiceRootURL . 'Image?$format=json&Query=';
$request = $WebSearchURL . urlencode( '\'' . $key . '\'');
$process = curl_init($request);
curl_setopt($process, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($process, CURLOPT_USERPWD, $accountKey . ":" . $accountKey);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($process);
$jsonobj = json_decode($response);
echo('<ul ID="resultList">');
foreach($jsonobj->d->results as $value)
{
echo('<li class="resultlistitem"><a href="' . $value->MediaURL . '">');
echo('<img src="' . $value->Thumbnail->MediaUrl. '"></li>');
}
echo("</ul>");
On running this program i am getting the results, I am getting the image source. But the thing is the image source doesn't seems to be a real image source i mean the URL is something like this :
http://ts4.mm.bing.net/th?id=HN.608026386931518543&pid=15.1
Also this link is expires after a month or so .... Initally i was able to see the image when clicking on the link but it expired after a month and now i can only see a greyish camera with a cross on it which means that the image source has been expired i guess.
If you can let me how can i restrict this thing and also is anything needs to done on the windows azure market place end to get things working for me .
Any help will be appreciated
Thanks

Fix for original images.
On the line 22, where are interpreted received and parsed JSON data
echo('<li class="resultlistitem"><a href="' . $value->MediaURL . '">');
there is a mistake from Bing.
You can just replace
$value->MediaURL for $value->MediaUrl
and you can get acquire access to original image.

Related

Filemaker api retruns 958 error code in php

I am trying to access FileMaker by using curl PHP. here I am not using filmmaker PHP class. I am importing the records from the filmmaker into the Prestashop site. when admin delete the product in the Prestashop its also deleted in the FileMaker database
$nome = 'http://ipaddress:port/fmi/xml/FMPXMLRESULT.xml?-db=tablename&-lay=export&recordid=$recordid&-delete';
$cURL = curl_init($nome);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cURL, CURLOPT_FAILONERROR, true);
curl_setopt($cURL, CURLOPT_HTTPHEADER, $header_fields);
$response = curl_exec($cURL);
if ($cURL_err = curl_errno($cURL)) {
die(__FILE__ . "(" . __LINE__ . "): Communication Error: (' . $cURL_err . ')" .
curl_error($cURL));
}
curl_close($cURL);
//Removed the Headers.
when I am executing the code filmmaker return error code 958.
after some analyze.
if we want edit,delete,update the record in the filemaker.we must use the -recid.
i made mistake on API
$nome = 'http://ipaddress:port/fmi/xml/FMPXMLRESULT.xml?-db=tablename&-lay=export&recordid=$recordid&-delete';
it should be
$nome = 'http://ipaddress:port/fmi/xml/FMPXMLRESULT.xml?-db=database&-lay=export&-recid=$recordid&-delete';
after changed its working correctly.Here I attached some example API for getting records and update the records.
GET all records
http://ip_address:port/fmi/xml/FMPXMLRESULT.xml?-db=database&-lay=layout&-findall
Get range of records
http://ip_address:port/fmi/xml/FMPXMLRESULT.xml?-db=Magazzino&-lay=export&-max=100&-findall
Get specific Record
http://ip_address:port/fmi/xml/FMPXMLRESULT.xml?-db=database&-lay=export&fieldname=field_value&-find
Insert New
http://ip_address:port/fmi/xml/FMPXMLRESULT.xml?-db=database&-lay=export&fieldname=field_value&-new
edit record
http://ip_address:port/fmi/xml/FMPXMLRESULT.xml?-db=database&-lay=export&-recid=record_id&-edit
NOTE: Two way to access filmmaker database 1.using FileMaker class.2.using API

Twilio lookup API not working?

I'm trying to use Twilio's Lookup API to get certain properties of a mobile number via PHP... with very little success:
$twilioClient = new Lookups_Services_Twilio(Credential::TwilioSID, Credential::TwilioToken);
$number = $twilioClient->phone_numbers->get($someNumber);
Note that this is the example code present within their 'Getting Started' page here.
By taking a look at $number in the debugger, I can confirm it is returning something:
The highlighted property of the object is simply recursive with no new information.
Attempting to evaluate $number->phone_number returns null. I have tried this with perhaps half a dozen completely valid numbers now and this is the only response I get.
Attempting to json_encode($number) returns false.
I have no idea why this is not working, but it'd be helpful if I could know what I'm doing wrong.
I would have been also not successful with their code defined so i used CURL to grab their API methods and it worked like a charm for me, you can try following code to get you need
$base_url = "https://lookups.twilio.com/v1/PhoneNumbers/+1XXXXXXXXXX";
$ch = curl_init($base_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$account_sid:$auth_token");
$response = curl_exec($ch);
$response = json_decode($response);
echo "<pre>"; print_r($response); echo "</pre>";
It will return you few parameters (country_code, national_format, carrier)
I'm just gonna go ahead and assume the phone numbers you've tried are neither from the US, nor in international format.
From Twilio's Lookups Quickstart Tutorial:
You'll want to include the country code of the phone number that you would like formatted. If not included, the country code will default to the US.
So your lookup should probably look like:
$number = $twilioClient->phone_numbers->get($someNumber, array('CountryCode' => 'NZ'));
If the phone numbers are from the US, in international format, or if the above still does not work, try whether the lookup succeeds on Twilio's web interface (you'll need the international prefix there).
If it does, your software library might be broken or your Twilio account might have incorrect/broken access rights.
If the web lookup fails as well, you should contact Twilio and report the issue.
Now 9-6-2016 and they still haven't fixed their PHP library...
None the less here is what worked for me. If you want more information like caller name etc you have to enable this in your twilio dashboard first.
require 'includes/twilio/Services/Twilio.php';
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "YOUR-SID";
$token = "YOUR-TOKEN";
$client = new Lookups_Services_Twilio($sid, $token);
// Lookup
$phoneNumber = rawurlencode("(000) 000-0000");
$full_path = $client->phone_numbers->uri . "/$phoneNumber" . "?CountryCode=US&Type=carrier&Type=caller-name";
$number = new $client->phone_numbers->instance_name($client, $full_path);
echo "Caller name:" . $number->caller_name->caller_name;
echo "<br>";
echo "Caller type:" . $number->caller_name->caller_type;
echo "<br>";
echo "Carrier type:" . $number->carrier->type . "\r\n";
echo "<br>";
echo "Carrier name:" . $number->carrier->name;
echo "<br>";
echo "Phone number:" . $number->phone_number;
echo "<br>";
echo "Country code:" . $number->country_code;

How can I upload the Facebook image in my "img" folder?

I'm using the Facebook php SDK to register my users with their Facebook account. I stored all the data I need in my sql database and I'm able to access the picture with:
<img src="https://graph.facebook.com/<?php echo $user;?>/picture">
I would like to store the image (the actual image not just the link) in one of my server folder so the user can change it without accessing his Facebook profile.
Is it possible to do it?
This should do what you want and put the image into images/facebook/[USERID].jpg.
Keep in mind that you should probably use cURL instead of file_get_contents so you can prepare for timeouts and long response times (which there WILL be with the facebook API.
<?php
$imageData = file_get_contents("https://graph.facebook.com/" . $user . "/picture");
file_put_contents("images/facebook/" . $user . ".jpg", $imageData);
?>
This is how you'd do it with cURL (untested, but should work):
<?php
$facebookCurl = curl_init();
curl_setopt($facebookCurl, CURLOPT_URL, "https://graph.facebook.com/" . $user . "/picture");
curl_setopt($facebookCurl, CURLOPT_HEADER, 0);
curl_setopt($facebookCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($facebookCurl, CURLOPT_TIMEOUT, 5);
$imageData = curl_exec($facebookCurl);
curl_close($facebookCurl);
file_put_contents("images/facebook/" . $user . ".jpg", $imageData);
?>

Amazon.com MWS Integration

I am currently developing a very basic site which will, at this time, simply display order information from Amazon's Marketplace.
I have all of the MWS Security Credentials.
I have downloaded and reviewed, with much confusion, the PHP Client Library.
I am kind of new to PHP but I feel like I can handle this project.
I need to know how to install and access information from this API. I feel like I've tried everything. Amazon does not supply enough information to get this going. They make it sound like it takes 5 or 6 easy steps and you can access your information; this is not true.
Is there a detailed tutorial on MWS? I need as much information as possible. If you can help me out, maybe outline the steps required to get it going, that would be very appreciated!!!! I'm pulling my hair out over this. Thanks again
A rough file to get you started. This is taken from several pages, including this one from #Vaidas. I don't have links yet, sorry. My only contribution is to put this together in one place.
None of the PHP code Amazon supplied worked for me out of the box. I'm assuming you have XAMPP with cURL or an equivalent environment. This code SHOULD work out of the box to get you started on what needs to happen. Just plug in your credentials.
<?php
$param = array();
$param['AWSAccessKeyId'] = 'YourAccessKeyID';
$param['Action'] = 'GetLowestOfferListingsForASIN';
$param['SellerId'] = 'YourSellerID';
$param['SignatureMethod'] = 'HmacSHA256';
$param['SignatureVersion'] = '2';
$param['Timestamp'] = gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time());
$param['Version'] = '2011-10-01';
$param['MarketplaceId'] = 'YourMarketplaceID';
$param['ItemCondition'] = 'new';
$param['ASINList.ASIN.1'] = 'B00C5XBAOA';
$secret = 'YourSecretKey';
$url = array();
foreach ($param as $key => $val) {
$key = str_replace("%7E", "~", rawurlencode($key));
$val = str_replace("%7E", "~", rawurlencode($val));
$url[] = "{$key}={$val}";
}
sort($url);
$arr = implode('&', $url);
$sign = 'GET' . "\n";
$sign .= 'mws.amazonservices.com' . "\n";
$sign .= '/Products/2011-10-01' . "\n";
$sign .= $arr;
$signature = hash_hmac("sha256", $sign, $secret, true);
$signature = urlencode(base64_encode($signature));
$link = "https://mws.amazonservices.com/Products/2011-10-01?";
$link .= $arr . "&Signature=" . $signature;
echo($link); //for debugging - you can paste this into a browser and see if it loads.
$ch = curl_init($link);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/xml'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
echo('<p>' . $response . '</p>');
print_r('<p>' . $info . '</p>');
?>
Please note that it is VITAL to have the
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
line, at least in my case. CURL was working fine for any page except for the MWS page (it was just giving me a blank page with -1s in the info, and it took me most of a day to figure out I needed that line. It's in the MWS forums somewhere.
For good measure, here's a link to MWS ScratchPad.
Once I get a better handle on working with MWS maybe I'll do a tutorial. Or someone who is better at HTML and has a need for more of the features could do it.
in case you still didn't figure out how to do this, follow these steps
read the Developer Guide
read the Reports API Reference
RequestReport with some ReportType that will return order data (page 51 or so, look the reports api reference)
you can test this with the MWS Scratchpad
you can also post to the Amazon MWS community forum to get additional help
you can even write to the Amazon Tech Support
hope this helps you and other users.
Amazon provides some great sample code at https://developer.amazonservices.com/. I've successfully used their code for my PHP applications.
I agree. It was a nightmare to figure out the MWS API.
Some changes to #Josiah's method to make it work for other marketplaces:
Line:
$sign .= 'mws.amazonservices.com' . "\n";
Change to: your correct MWS endpoint. List here http://docs.developer.amazonservices.com/en_US/dev_guide/DG_Endpoints.html - it'll match your marketplace ID, which could be something like this:
$sign .= 'mws-eu.amazonservices.com' . "\n";
and UK marketplace ID for UK site.
Line:
$link = "https://mws.amazonservices.com/Products/2011-10-01?";
Again, change the start of the url in line with above.
This'll probably give you straight text output in a browser (view source for xml). For XML visible output (easier for checking) do this:
Add an XML content type line to top of file:
header('Content-type: application/xml');
Then comment out:
echo($link);
and
print_r('<p>' . $info . '</p>');
Implementing MWS is easy if you follow the right steps:
1-Download the codebase library from the https://developer.amazonservices.com/ as per your preferred language.
2-Set your seller mws credentials in config.php file under sample folder so that same can be used while running the specific file under the sample folder like: RequestReportSample.php and set the report type and endpoint url for specific seller domain.
3- You can then check submitted request status from scratchpad.
4- You can use GetReportSample file to get the order report data and use the same as per your need.
You can follow the reference as well http://prashantpandeytech.blogspot.com/2015/03/mws-amazon-marketplace-web-service-api.html

How to use twitter api with "http basic auth"?

How to use twitter api with "http basic auth"?
I think I should use the "consumer key"! because twitter gave you limit rate of requests per hour, how can they count my requests if I didn't use my consumer key?
Whenever you want to use HTTP basic auth with anything, if you want to ignore the actual implementation and HTTP headers, just use cURL. Here's a simple example in PHP, cURL is available in other languages too:
<?php
$ch = curl_init();
// Sets the URL cURL will open
curl_setopt($ch, CURLOPT_URL, 'http://twitter.com/statuses/user_timeline.xml?screen_name=al3x');
// Here's the HTTP auth
// The 3rd argument is your Twitter username and password joined with a colon
curl_setopt($ch, CURLOPT_USERPWD, 'username:password');
// Makes curl_exec() return server response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Lately the Twitter API expects an Expect header. It's a mystery
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
// And here's the result XML
$twitter_xml = curl_exec($ch);
curl_close($ch);
?>
And then $twitter_xml will contain the XML of al3x's public timeline. As far as rate limiting goes, ceejayoz already answered that pretty well.
Authenticated API GET requests are counted against your user account's tally.
Unauthenticated API GET requests (permitted by some methods) are counted against your IP address's tally.
POST requests are not rate limited.
More details are available in the Twitter docs.
I would think that they count the requests from the same IP - but I haven't double checked.
Even dynamic IP address will be static for a session and, in the absence of any other identifying information, the only thing that will distinguish you from other users.
As other posters have said - there are better methods if you've got a key.
$twitter = file_get_content("http://user:password#twitter.com/blabla");
more about native HTTP Wrapper support in PHP
I recently wrote some PHP to post to Twitter
This is the working part of it:
$message = 'A new revision (#' . $data['revision'] . ') was commited by ' . $data['author'] . ': ' . $data['message'] . "";
$message = substr($message, 0, 140);
$content = 'status=' . urlencode($message);
$packetString = "POST /statuses/update.xml HTTP/1.1\r\n";
$packetString .= "Authorization: Basic " . base64_encode($username . ":" . $password) . "\r\n";
$packetString .= "Content-Length:" . strlen($content) . "\r\n";
$packetString .= "HOST: twitter.com\r\n";
$packetString .= "\r\n" . $content . "\r\n";
$sock = fsockopen('twitter.com', 80);
fwrite($sock, $packetString);
//This is some logging, to a local file so I can monitor local what's going on
$response = fread($sock, 10240);
fwrite($fh, $packetString . "\r\n\r\n\r\n" . trim($response) . "\r\n\r\n\r\nD:\r\n" . $d);
fclose($fh);
You can see it in action here: http://twitter.com/fmsvn using a callback from our SVN server I am posting the SVN messages to the projects Twitter Feed.

Categories