IBM Watson retuning empty response - php

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.

Related

file get content google geocode not working

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!

Getting all docs from Cloudant with PHP and CURL

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?

PHP JSON parsing with cURL

I want to create a simple page that prints the region you are in, I am using an API from this site freegeoip.net. I have it set up so it runs a users ip through the site and returns JSON, however I am having issues parsing that response. This is the code I have written:
<?php
$person = $_SERVER["REMOTE_ADDR"];
$url = "freegeoip.net/json/$person";
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_URL, $url);
curl_setopt($cURL, CURLOPT_HTTPGET, true);
curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Accept: application/json'
));
$result = curl_exec($cURL);
curl_close($cURL);
$json = json_decode($result, true);
echo $json['region_name'];
echo $json['city'];
?>
However for some reason, it still prints the full response from the server API... how do I fix this?
Add the following line after setting CURLOPT_HTTPHEADER:
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);
By default curl_exec() outputs the response out directly and it returns TRUE on success or FALSE on failure. If you set CURLOPT_RETURNTRANSFER to TRUE, curl_exec() will return the actual result of successful operation, but still will return FALSE on failure.

Send and Receive JSON using an https curl_exec call in PHP

I'm trying to make a curl call using PHP. My project has a flow like this
I redirect user to the server.
User uses the services and the server redirects him to a page on my application.
I would like to make a server to server call at this point to verify certain details.
Point to be noted - I post JSON data as well as receive JSON data as response.
I have tried this. I am able to send data but don't get any response.
Is this correct?
Please help!
$data = array("One" => "Onedata", "Two" => "Twodata");
$JsonData = json_encode($data);
$ch = curl_init('https://exampleurl');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $JsonData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($JsonData))
);
$result = curl_exec($ch);
As you mention in the comment:
The problem is with SSL verification of the host. It takes some effort (see http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/) to setup SSL correctly, so a workaround is to add the following two options:
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
Try to use the below code which works for me. and make sure the json_url is correct. When you run it on the browser you should get an output. Before try it using CURL, run in on the browser and see whether you get the output you want.
$json_url ='https://exampleurl';
// Initializing curl
$ch = curl_init( $json_url );
// Configuring curl options
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-type: application/json')
);
// Setting curl options
curl_setopt_array( $ch, $options );
// Getting results
$results = curl_exec($ch); // Getting jSON result string
$json_decoded = json_decode($results);

How to parse Curl response?

I am able to send the GET request and receive the response at following line.
$curl_resp = curl_exec($curl);
I used the following to parse the response, but it does not work, I have manually set some values to $curl_resp but still not sure how to access the value of each tag of the xml separately.
$xml = simplexml_load_string($curl_resp);
NOTE: I recevice the actual xml but cant parse it, (I need to get each tag's value separately in a variable)
Code:
<?php
$service_url = ' The Url goes here';
$curl = curl_init($service_url);
$curl_post_data = array(
"PASSWORD" => 'pass',
"USERNAME" => 'username'
);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
$curl_resp = curl_exec($curl);
curl_close($curl);
Your variable $curl_response is different than $curl_resp (what you're trying to parse)
You can access the value of each tag just like any other array.
if the CURLOPT_RETURNTRANSFER option is not set then your $curl_resp will just return true/false.
if it is set you may be returning false or a poorly formed xml string. If you post more code or the actual curl response text we may be able to provide more info.
EDIT:
upon reading the code looks like you are assigning the response text to $curl_response instead of $curl_resp
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, "your url");
curl_setopt($ch, CURLOPT_USERAGENT, 'Googlebot/2.1 (+http://www.google.com/bot.html)');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);

Categories