When i try to execute the code below, it dont work.
$json = file_get_contents('https://maps.google.com/maps/api/geocode/json?address=R%20GRANJAO,%2021&sensor=false&key=MYAPIHERE');
var_dump($json);
The code above return me with "ZERO_RESULTS".
But if i manualy access the link, it returns the contents.
Example below:
Of course i am using a valid API, because most of the addresses work, but a few, just like this example, dont.
I tried to use urlencode() as well, but it doesnt seens to be the problem here.
I seens that it was necessary to add a header setting 'accept-language'.
$url = "https://maps.google.com/maps/api/geocode/json?address=".urlencode($address)."&sensor=false&key=$apiKey";
$header = array('Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3');
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
$response = json_decode($json, true);
This solved the issue!
Related
I have a simpel php Curl call to get something from curl natural languauge understanding.
This is my code:
$report = strtoupper($report);
$username = 'xxx';
$password = 'xxx';
$url = 'https://gateway.watsonplatform.net/natural-language-understanding /api/v1/analyze?version=2017-02-27&text=Helloethics&features=entities,sentiment,keywords';
// Set post arguments for call
$post_args = array(
'text' => $report
);//Set header arguments for call
$header_args = array(
'Content-Type: text/plain',
'Accept: application/json'
);// Set options for REST call via curl
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_USERPWD, "xxx:xxx");
curl_setopt($curl, CURLOPT_HTTPHEADER, $header_args);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_args);
$status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);print_r($status_code);
// Actual REST call via curl and cleanup (closing) of curl call
$result = curl_exec($curl);
echo "print:";print_r($result);
curl_close($curl);
But it just brings me an empty respons. What do I do wrong? And how can I fix this?
It's possible the extra space in your $url variable is causing problems.
However, if you're sure that's not it, try using var_dump($result) instead of print_r($result).
You're accepting JSON data which initially comes back as a string. I think var_dump() will confirm you need to use $resultArray = json_decode($result, true); before you can use print_r to display your cURL response.
I'd also recommend adding <pre></pre> tags around your output for readability... personal preference though.
If the above solution isn't working, what do you get when you do var_dump($status_code)?
TLDR - always use var_dump() to debug, it can save you on more occasions than you expect.
I have created a call back API to receive the JSON data after hitting my server. But somehow its not working as expected
This the response which i have to receive in my API
{"ERROR":0,"STATUS":1,"ORDERID":753093,"OPTRANSID":"2107938600"}
I have written the php file for this rcstatus.php
$url = 'http://softwarecompany.club/pks/recharge/b2b/rcstatus.php?status='.$_GET['status'].'&Orderid='.$_GET['Orderid'].'&Optransid='.$_GET['Optransid'];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 30000);
$data = curl_exec($ch);
echo $data;
$json = json_decode($url,true);
$json['STATUS'];
Please help me to achieve this. I have never worked with JSON data before. So please help me to achieve it. Thanks in advance!!!
You need to replace:
$json = json_decode($url,true);
with
$json = json_decode($data,true);
Replace $url to $data in last when you get the response like this.
$url = 'http://softwarecompany.club/pks/recharge/b2b/rcstatus.php?status='.$_GET['status'].'&Orderid='.$_GET['Orderid'].'&Optransid='.$_GET['Optransid'];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 30000);
$data = curl_exec($ch); // This is Your Response
$json = json_decode($data,true);
$json['STATUS'];
<?php
$url = 'https://adidas.com/us/';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$data = curl_exec($curl);
curl_close($curl);
echo $data;
?>
I'm just trying to display the contents via cURL, and when I try https://www.google.com/ or another website, it works just fine, however on the adidas site, it timesout?
Thanks!
Add a curl_getinfo call and you'll see more useful information
<?php
$url = 'https://adidas.com/us/';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$data = curl_exec($curl);
print_r(curl_getinfo($curl));
curl_close($curl);
echo $data;
?>
In this case, after a minute it times out and curl_getinfo returns http_code of 0. This means that a connection can not be established.
adidas.com is apparently inaccessible through HTTPS. It works through HTTP and redirects to http://www.adidas.com/us/. This address works through HTTPS too.
Use https://www.adidas.com/us/ instead.
You get an empty string for curl_exec (for http link) because response code is 301 Moved Permanently. Body of the HTTP response is empty.
I'm trying to fetch all the docs from my CouchDB hosted in Cloudant, using PHP and CURL.
So far I've tried this, I get a 200 status, but nothing on the Response column of the console.
<?php
$url = "https://myuser.cloudant.com/mydb/_all_docs?include_docs=true";
$user = 'myuser';
$pass = 'mypass';
$ch = curl_init(); // initialize curl handle
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$user:$pass");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
return $output;
return json_decode($output,true);
curl_close($ch);
?>
I'm not fluent in PHP, what am I missing?
A couple of things look odd to me about the above code sample. I usually put the credentials in the URL so https://user:pass#myuser.cloudant.com as the URL - then you don't need the following two curl_setopt lines. I'm not exactly sure how it would work to pass those separately.
Also you're returning twice. If you just want to inspect the output try the var_dump() command and see if that shows you what you expect?
i am trying to get the content of this json: http://steamcommunity.com/market/pricehistory/?country=DE¤cy=3&appid=730&market_hash_name=Chroma%20Case
This is my code:
$url = "http://steamcommunity.com/market/pricehistory/?country=DE¤cy=3&appid=730&market_hash_name=Chroma%20Case";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIE, 'steamLogin = 76561198075419487%7C%7C3F1A776553C4BE1D0F6DA83059052E79DB7EB3C7');
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
$json_string = json_encode($output, JSON_PRETTY_PRINT);
When printing out $json_string it results in nothing, $output results in "‹ŠŽÿÿ)»L". I would like to grab the actual content on the website, the steamLogin-Cookie is needed for that. The cookie that's stored in my browser at the moment is the one I hardcoded in the source.
If you need any more info, feel free to ask.
Adding curl_setopt($ch, CURLOPT_ENCODING,""); made it :)