yahoo maps api curl settings - php

How should I configure cURL to retrieve data from the yahoo maps api?
Here is the current code
curl_setopt($ch, CURLOPT_TIMEOUT, 5000);
curl_setopt($ch, CURLOPT_URL, $geocodeurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
Which returns a 400 - Bad request HTML file. I've checked $geocodeurl and it is a valid XML file, so I figure the problem must be the cURL options?
$geocodeurl is
http://where.yahooapis.com/geocode?appid=** My App ID **&q=Battle%20Creek,MI&gflags=R

i wrote a simple class wrapper to get a basic address as a php object, which might help you get the job done! i also added a google geocoding wrapper.
to answer your question, everything is ok with the url you posted, but yes as you mentioned on your reply you should urlencode the params
$query = $this->url."?appid=".$this->appid."&flags=".$this->format;
$query .= "&location=".urlencode($address);
here is a link to my wrapper class
https://github.com/mrpollo/Geocoding-API

OK as usual I had misidentified the problem. cURL was fine, but the q= variable was not being passed through any sort of urlencode function correctly.
Worked in my browser because Firefox kindly changed the to %20. With cURL you need to be more careful. . . .

Related

cURL request getting wrong request version in Google App Engine

I have a cURL request in my code which works fine when running locally:
$url = "http://ipinfo.io/{$_SERVER['REMOTE_ADDR']}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$response = curl_exec($ch);
$locale = json_decode($response);
and returns a JSON as expected. Our production system is on Google App Engine, however, where I get the website version for a browser rather than the JSON.
I can get this cURL request to work if I change
google_app_engine.enable_curl_lite = "1"
in the php.ini in the root directory of my project to
extension = "curl.so"
but Google's documentation insists the former is to be used on production. Additionally, using the latter breaks things like Monolog's SlackHandler.
Is there a way to get the JSON from this cURL request while still using Google's "cURL Lite"?
From the ipinfo.io documentation:
"We do a little bit of magic on the server to determine if we should send the JSON response or the webpage. We usually get it right, but if you're seeing the webpage instead of the JSON (or want to check the JSON output in a browser) you can force the JSON response by adding /json to the end of the URL"
Adding /json to the end of the URL worked for me in this case, but I wanted a more general solution. Since Google's cURL Lite uses their URL Fetch in the background, ipinfo.io's "magic" is somehow getting confused. I found that by specifying the Accept header then the /json addition wasn't required. In PHP:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
Thanks to the folks on the #php channel of NashDev Slack for helping me on this one!

Google feed api not returning results with php in wordpress

i am trying to use the google feed api in my wordpress site. i have enabled php with a plugin, which allows me to input php code in my pages. my hosting provider also confirmed they have curl enabled.
This is the code which iam trying to run which i got from the google developer site
(https://developers.google.com/feed/v1/jsondevguide#basic_query)
<?php
$url = "https://ajax.googleapis.com/ajax/services/feed/find?v=1.0&q=iphone5& userip=2.96.214.41";
// sendRequest
// note how referer is set manually
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, http://www.iweb21.com);
$body = curl_exec($ch);
curl_close($ch);
// now, process the JSON string
$json = json_decode($body);
// now have some fun with the results...
?>
i don't get any results, just a blank page.
i am not a php programmer. just a novice wordpress user. i have been searching for a plugin to use the google feed api but got nowhere. so i decided to try using the code provided by google.
i Would very much appreciate any advise. thnx
Blank page means that there is a Fatal or Parse error, and error reporting is disabled in PHP settings. See this: How to get useful error messages in PHP?
In your particular case, the referer string is not enclosed in quotes and generates a Parse Error. Replace with:
curl_setopt($ch, CURLOPT_REFERER, 'http://www.iweb21.com');

Skydrive: Listing root files and folders in PHP

We would like to integrate Skydrive in our website, like we're currently doing with Google Drive but I'm lost in how to retrieve the root folders and files.
I've read these posts and docs:
Access SkyDrive using PHP and OAuth
Convert to PHP REST CURL POST
Skydrive API here: http://msdn.microsoft.com/en-us/library/live/
Curl doc on http://php.net - even if I'm still confused about that
And yet, I can't seem to get what I wanted.
I've already the access_token and it's correctly assigned, the scopes are also correct (I'm using wl.contacts_skydrive), so, I'm putting here part of the code I've so far so you can tell me what I'm missing, what I should add or if I'm taking a wrong turn.
//$token is correctly assigned
$url = 'GET https://apis.live.net/v5.0/me/skydrive/files?access_token='.$token;
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
Thanks you for your time taken to help me.
You need to use CURLOPT_RETURNTRANSFER to return the transfer as a string.
Otherwise you will get true or false. Assuming you were getting true from curl_exec
Try adding
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

new to curl & PHP: how to use it

I am trying to access some basic info from an XML based API. I'm new to this and am getting lost reading the guides on php.net and via google that seem to assume I already know the basics.
The input needs to be formatted as follows:
<query>
<auth_key>xxxxx</auth_key>
<command>get_account</command>
<account_id>11122</account_id>
</query>
The return will be in an XML format. I assume I need to use CURL to connect and send the input, but I'm lost - how should the PHP code look to do this?
::UPDATE::
okay, I'm still struggling and not making any progress. I've found a tutorial that has kinda lead me to the following code, but it's not doing anything and I can't figure out how/where I'm supposed to acutally send the XML data through to the url.
$URL = 'http://www.test.com';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
print_r($data);
I'm not getting this right and not sure what I'm supposed to be doing. Any help would be appreciated!
The API probably expects you to POST your request to the server, in which case an HTTP POST with cURL example should help. Check the HTTP code of server response (200 is good, others are probably bad) - and if it's good then parse the XML.
Most APIs have very good documentation and examples though. It's worth reading through them or Googling for other people in your shoes.
I ended up stumbling on the following link: http://www.phpmind.com/blog/2009/08/how-to-post-xml-using-curl/
From there I got the sample code working and manipulated it to suit my needs. I also ended up using http://php.net/manual/en/book.simplexml.php for the xml parsing and it seemed very straight forward.

CURL Request Issue

So I'm a beginner to all of this. I'm attempting to teach myself how to properly use CURL, and dabble a bit in API calls (don't worry, this question only relates to using CURL).
Now my original code worked just fine - I structured the request using urlencode, and the vast majority of requests leaded to the responses I was looking for. About 10% of the received responses, however, that wouldn't retrieve the results I needed. Let me give an example:
$urlToFetch = "http://lsapi.seomoz.com/linkscape/url-metrics/" . urlencode($objectURL) . "?" . $this->structureURL();
$response = ConnectionUtil::makeRequest($urlToFetch);
Using the above code would generate a $urlToFetch with the following data:
http://lsapi.seomoz.com/linkscape/url-metrics/www.alinki.com%2Fdomainlinks.php%3Fpage%3D516000?AccessID=XXXXXX&Expires=1286388878&Signature=YYYYYYYYY
This passes through CURL, and gets a response from the seoMoz API - the response, however, is missing some data. I've poked around, and found that it is an issue with how the URL is structured. For example, by changed the value from www.alinki.com%2Fdomainlinks.php%3Fpage%3D516000 to www.alinki.com/domainlinks.php?page=516000 I can retrieve all the fields I am looking for. Going by that logic, I should be able to structure a URL similar to this:
(I'm removing the http since I cannot post more than one link) lsapi.seomoz.com/linkscape/url-metrics/www.alinki.com/domainlinks.php?page=516000?AccessID=XXXXXX&Expires=1286388878&Signature=YYYYYYYYY
and retrieve all the fields I need. The problem is, when I attempt to pass that URL through to CURL, it will not complete the request. Below is the code used for the CURL request:
public static function makeRequest($urlToFetch)
{
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, "$urlToFetch");
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, ConnectionUtil::CURL_CONNECTION_TIMEOUT);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
$buffer = curl_exec($curl_handle);
//var_dump($buffer);
curl_close($curl_handle);
$arr = json_decode($buffer);
return $arr;
}
And I request as follows:
$urlToFetch = "http://lsapi.seomoz.com/linkscape/url-metrics/" . urlencode($objectURL) . "?" . $this->structureURL();
$response = ConnectionUtil::makeRequest($urlToFetch);
Any ideas on what silly mistake I am making?
Try using http_build_query instead of urlencode for generating your query string. It looks like the seomoz code is not handling the urlencoded data properly.

Categories