getting no response from php google geocode rest api request - php

I'm getting no response and no errors from this php code. Does anyone know what I'm doing wrong, please? It seems straightforward:
php:
$details_url = "https://maps.googleapis.com/maps/api/geocode/json?address=436+Grant+Street+Pittsburgh&sensor=false&key=mykey";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$response = curl_exec($ch);
print_r($response);

You never bothered to tell curl about your url. You should have
$ch = curl_init($details_url);
^^^^^^^^^^^^
or
curl_setopt($ch, CURLOPT_URL, $details_url);
And note that print_r is not a good debug tool. You probably got a boolean false from curl_exec, which print_r won't display at all:
php > $x = false;
php > print_r($x);
php > var_dump($x);
bool(false)
A better option would be
$response = curl_exec($ch);
if ($response === false) {
die(curl_error($ch));
}

Related

Check response status code with PHP / cURL (file_get_contents_curl)

I use this function (from here) with cURL in PHP:
<?php
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
This works quite well - - for example, that way I can get the 'body' of a JSON. Like this:
<?php
$crossrefurl = 'https://api.crossref.org/works/10.1163/1871191X-13020004';
$obj = file_get_contents_curl($crossrefurl);
# convert to json
$obj = json_decode($obj, true);
What I don't know is: how can I get the response status code (given that my ´curl`-commands are in a function)?
Specifically, I want to find out whether the response status code is 429 in which case I would let the script sleep for 5 seconds (to avoid being rate-limited).
According to this post, the code should be something like:
<?php
if(($httpcode == 429)) { sleep(5); }
... but how do I get to $httpcode in the first place?
Thanks for your help!

Json returns null value

I want to get Json data from Carquery API. But none of my code worked.
First i tried to get the data using curl. This is my code
$loginUrl = 'http://www.carqueryapi.com/api/0.3/?callback=?&cmd=getTrims&year=2015';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$loginUrl);
$result=curl_exec($ch);
curl_close($ch);
var_dump(json_decode($result));
It returned null. And then i tried using file_get_content. This is my code :
$json = file_get_contents('http://www.carqueryapi.com/api/0.3/?callback=?&cmd=getTrims&year=2015');
$obj = json_decode($json);
var_dump($obj);
Unfortunately it didn't work either.
Any help please?
First of all try this:
$loginUrl = 'http://www.carqueryapi.com/api/0.3/?callback=?&cmd=getTrims&year=2015';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$loginUrl);
$result=curl_exec($ch);
if($result === false)
{
echo 'Curl error: ' . curl_error($ch);
}
else
{
var_dump(json_decode($result));
}
curl_close($ch);
so you can see error if any on curl request.
and 2nd thing that url:
'http://www.carqueryapi.com/api/0.3/?callback=?&cmd=getTrims&year=2015'
are you sure that the must be 2 (two) ? (question marks) ?
json data return from that API is not correct, i tried using jsonlint.com to check it.
If the callback part is removed from
'http://www.carqueryapi.com/api/0.3/?callback=?&cmd=getTrims&year=2015';
into
'http://www.carqueryapi.com/api/0.3/?cmd=getTrims&year=2015';
it will produce right json and can be parsed either using file_get_content() or curl.

No response from XML using cURL

I am using a simple cURL statement to parse XML on my site. When the API is up and working it works fine, however as soon as the API does down for any reason the entire site crashes.
$url = 'http://www.mydomain.com/webservicexample';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$xml = new SimpleXmlElement($data);
Is there a conditional I can put around the url so that it only carries out the cURL script if there's a positive response from the API? I tried the following but it didn't work because it never got a server response to give any headers:
$url_headers = #get_headers($url);
if($url_headers[0] == 'HTTP/1.1 200 OK') {
// do script
}
Any help/advice much appreciated!
You can check the return value of curl_exec():
if (false === ($data = curl_exec($ch))) {
die("Eek! Curl error! " . curl_error($ch));
}
And check the response headers too:
if (200 !== (int)curl_getinfo($ch, CURLINFO_HTTP_CODE)) {
die("Oh dear, no 200 OK?!");
}
In the end I was able to get it working by setting a timeout time with CURLOPT_TIMEOUT and CURLOPT_CONNECTTIMEOUT and then put a conditional around it using curl_errno().
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
$data = curl_exec($ch);
if(!curl_errno($ch))
{
curl_close($ch);
$xml = new SimpleXmlElement($data);
return $xml;
}

Using cURL to get a URL that is returning JSON

All,
I have the following code:
$c = curl_init();
curl_setopt($c, CURLOPT_URL, "https://api.twitter.com/1/statuses/user_timeline.json?screen_name={$username}&count={$how_many}");
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec($c);
curl_close($c);
if ( ! empty( $contents ) ) {
// Decode it.
echo "it is in here";
$tweet = json_decode( $contents );
}
This code never gets into the if statement because it isn't returning any results. Any idea on how to get this to return all of the results?
Thanks!
Looks like your server doesn't trust Twitter's certificate authority (you're probably getting an SSL error but not seeing it due to your error reporting settings).
Follow this guide to get it working - http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/
Try this:
$c = curl_init();
curl_setopt($c, CURLOPT_URL, "https://api.twitter.com/1/statuses/user_timeline.json?screen_name={$username}&count={$how_many}");
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec($c);
if(curl_exec($ch) === false)
echo 'ERROR: '.curl_error($ch);
curl_close($c);

cURL on PHP 5.1.4 not returning anything from xml feed

I am trying to get the code below working on a server running PHP 5.1.4 but it does not appear to be returning anything; print_r($buffer); displays nothing and var_dump($buffer); returns "bool(false)". It works on servers running PHP 5.2.x and 5.3.2 though..
error_reporting(E_ALL);
$ch = curl_init("http://api.twitter.com/1/statuses/user_timeline/xxxxxxxxxxxxxx.xml");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, FALSE);
$buffer = curl_exec($ch);
curl_close($ch);
print_r($buffer);
var_dump($buffer);
curl_exec returns boolean FALSE when an error occurs. Try doing:
$buffer = curl_exec($ch);
if ($buffer === FALSE) {
die(curl_error($ch));
}
which'll spit out the error message/code for you.

Categories