I would like to access IBM Tone Analyzer through PHP using CURL, however, I keep getting authorization errors like shown below. The code I use is shown in the code section.
Although looking up numerous help pages and forums, I haven't found a suitable solution. Could someone please help me how to solve this?
Thanks a lot in advance.
With best regards!
This is the code i use:
<?php
//print_r($_POST);
$ch = curl_init();
$file_path = "tone.json";
$url = "https://api.eu-gb.tone-analyzer.watson.cloud.ibm.com/instances/$BLACKENED$/v3/tone?$
$header_args = array(
'Accept: application/json',
'Content-Type: application/json',
'Authorization: Basic apikey:$BLACKENED$'
);
curl_setopt_array($ch,array(CURLOPT_URL => $url, CURLOPT_HEADER => $header_args, CURLOPT_UPLOAD => true, CURLOPT_POST => true, CURLOPT_INFILE => $file_path));
$response = curl_exec($ch);
echo($response);
curl_close($ch);
?>
This is the error i get:
HTTP/1.1 401 Unauthorized strict-transport-security: max-age=31536000; includeSubDomains; WWW-Authenticate: Basic realm="IBM Watson Gateway(Log-in)" Content-Length: 164 Content-Type: application/json x-dp-watson-tran-id: $BLACKENED$ x-request-id: $BLACKENED$ x-global-transaction-id: $BLACKENED$ Server: watson-gateway X-EdgeConnect-MidMile-RTT: 0 X-EdgeConnect-Origin-MEX-Latency: 380 Date: Mon, 06 Dec 2021 21:46:28 GMT Connection: close {"code":401,"more_info":"https://cloud.ibm.com/docs/watson?topic=watson-authorization-error","error":"Unauthorized","trace":"$BLACKENED$"} 1
Related
I desperately want to use an API that asks curl. I do not know anything at all. So I teter to document myself, but I do not understand everything.
Here is what the API says:
All requests must be provided with unique API keys, which you can generate in dashboard. X-API-ID (public) and X-API-KEY (private) parameters. For example:
curl -L http://www.coinimp.com/api/v2/hashes
-H 'X-API-
ID:7e26bb94aa2ce44e6e16aca6ae6d28c7f0157b5ccd7a82f86bbbe8d835effd71'
-H 'X-API-
KEY:5112486af64b2f97bd3742c4153cee32452549491480cfd164b336720b82a84d'
Here is my code:
$curl = curl_init();
$opts = array(
CURLOPT_URL => 'http://www.coinimp.com/api/v2/hashes',
CURLOPT_HEADER => array(
'X-API-
ID:0cd6929b8e34e2cc686eb50bef6a909c4898125b5105221fbfe48a43b038d9ff',
'X-API-
KEY:61dbf2d44abd138bad67c7876dcac0f58b2f08c8bbb91108c7c0984fe7b5f207',
)
);
curl_setopt_array($curl, $opts);
$response = json_decode(curl_exec($curl), true);
print_r($response);
Here is my result:
HTTP/1.1 301 Moved Permanently Date: Sat, 16 Feb 2019 20:19:44 GMT Transfer-Encoding: chunked Connection: keep-alive Cache-Control: max-age=3600 Expires: Sat, 16 Feb 2019 21:19:44 GMT Location: https://www.coinimp.com/api/v2/reward Server: cloudflare CF-RAY: 4aa2b5fd8a25c83d-AMS 1
Please can you help me ?
CoinImp is an absolute mess; I worked with them briefly on a client project and quickly came to dislike their service. You're receiving a 301 response, which indicates that the resource you're requesting is no longer at that address. In the response it appears to give you the new endpoint:
https://www.coinimp.com/api/v2/reward
I would try reformatting your request towards that endpoint. Otherwise, this would be a question for CoinImp support.
I don't know your problem, but i suggest use to use Postman Api Development Environment.
https://www.getpostman.com/
You should enter URL where you will make the request, username, password and additional parameters specified by API provider.
You can test API also you can generate script in different programming languages without writing a single row of code.
Hope this helped you.
Try this:
Replace WEBSITE-ID with the ID you used for miner script.
And replace PUBLIC-ID and SECRET-ID, also USER-ID.
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://www.coinimp.com/api/v2/user/balance?site-key=WEBSITE-ID&user=USER-ID",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"x-api-id: PUBLIC-ID",
"x-api-key: SECRET-ID"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
Good luck.
I'm trying to set up an API call through php using cURL. The API documentation gives an example for a call to this API from Java:
HttpResponse<JsonNode> response = Unirest.get("https://api2445582011268.apicast.io/games/1942?fields=*")
.header("user-key", "*******")
.header("Accept", "application/json")
.asJson();
This is my attempt at converting it to a cURL call in php:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api2445582011268.apicast.io/games/1942?fields=*");
curl_setopt($ch, CURLOPT_HEADER, array(
"user-key: *******",
"Accept: application/json"
));
$output = curl_exec($ch);
echo $output;
curl_close($ch);
However my php is echoing the following output:
HTTP/1.1 403 Forbidden Content-Type: text/plain; charset=us-ascii Date: Sat, 02 Sep 2017 02:52:40 GMT Server: openresty/1.9.15.1 Content-Length: 33 Connection: keep-alive Authentication parameters missing1
Does anyone know how to solve this?
You used CURLOPT_HEADER (which is a Boolean to indicate you want the headers in the output), but you need CURLOPT_HTTPHEADER (which is used to pass an array with the headers for the request):
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"user-key: *******",
"Accept: application/json"
));
I'm tasked with Posting to an API that only accepts JSON. I am using PHP's CURL to accomplish this.
I have posted to API's before with no issue, but never with JSON, something I am not familiar with, I have tried to research this on my own and solve the problem, but I'm not having any luck.
When talking with someone at the company I am trying to post to, I was told that my server is hitting their server, the only thing wrong is the body of my post is not properly formatted JSON.. (no one at this company knows php :( so no help there)
Here is my code:
$jarr = array("ProviderID" => "L005A", "FirstName" => $first,
"LastName" => $last, "PhoneNumber" => $PhoneNumber,
"PhoneNumberType" => $PhoneNumberType);
$content = json_encode($jarr);
$curl_handle = curl_init("URL Im posting to");
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array(
'Accept: application/json;charset=utf-8',
'Content-Type: application/json;charset=utf-8',
'Expect: 100-continue',
'Connection: Keep-Alive'));
curl_setopt($curl_handle, CURLOPT_HEADER, 1);
curl_setopt($curl_handle, CURLOPT_POST, 0);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $content);
curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
$first = curl_exec($curl_handle);
curl_close($curl_handle);
echo "Result ";
echo $first;
From the documentation I was given, the only parameters I must follow are:
Method: Post
Headers:
Accept: application/json;charset=utf-8
Content-Type: application/json;charset=utf-8
Expect: 100-continue
Connection: Keep-Alive
Currently, when I execute the script, I get a return of:
Result HTTP/1.1 100 Continue HTTP/1.1 400 Bad Request Cache-Control:
no-cache Pragma: no-cache Content-Type: application/json;
charset=utf-8 Expires: -1 Server: Microsoft-IIS/7.5 X-AspNet-Version:
4.0.30319 X-Powered-By: ASP.NET Date: Thu, 01 Nov 2012 15:43:18 GMT Content-Length: 26 {"record.ProviderID":[""]}
So the 400 error is caused because my body is not properly formatted. My question, how would I format the data I need to send in proper JSON format?
From what I have read, this code should do it, but it does not:
$jarr = array("ProviderID" => "L005A", "FirstName" => $first,
"LastName" => $last, "PhoneNumber" => $PhoneNumber,
"PhoneNumberType" => $PhoneNumberType);
$content = json_encode($jarr);
I faced the same problem. After debugging, found out that while sending JSON string, not quoting or double quoting the Integer values were the culprit. You need to send double quoted integer values as a string.
Eg:
{"PhoneNumber":9876543210}
or
{"PhoneNumber":"9876543210"}
will not work.
but
{"PhoneNumber":"N_9876543210"}
will work.
Will update as soon as I figure it out what exactly is the root cause of this problem. If someone knows, please update soon.
Edit: The problem solved by URL Encoding. I hope it solves in your case also.
I'm trying to load URLs using Coral CDN but I could not make it work if I use my own context resource.
<?php
$url = 'http://www.stackoverflow.com/';
echo substr(htmlentities(file_get_contents($url)),0,100); // works OK
echo '<hr />';
$url = 'http://www.stackoverflow.com.nyud.net/'; // CORAL content distribution network
echo substr(htmlentities(file_get_contents($url)),0,100); // works OK
echo '<hr />';
$options = array(
'http'=>array(
'method'=>"POST",
'header'=>
"Host: www.stackoverflow.com.nyud.net\r\n".
"Connection: keep-alive\r\n".
"Content-Length: 3\r\n".
"Cache-Control: max-age=0\r\n".
"Origin: http://www.stackoverflow.com.nyud.net\r\n".
"User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1\r\n".
"Content-Type: application/x-www-url-form-encoded\r\n".
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n".
"Accept-Encoding: gzip,deflate,sdch\r\n".
"Accept-Language: en-US,en;q=0.8\r\n".
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3",
'content'=>'a=1'
));
$context = stream_context_create($options);
echo file_get_contents($url,false,$context); // 405 error?
?>
This is the actual error I get:
Warning: file_get_contents(http://www.stackoverflow.com.nyud.net/) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 405 in C:\...\lab\php-exec\index.php(4) : eval()'d code on line 29
I know curl might work but I insist to use file_get_contents(), how do you think can I solve this problem?
Thanks!
Well, I think you just can't send a POST request to *.nyud.net website, see a basic post.
Request:
POST / HTTP/1.1
Host: www.stackoverflow.com.nyud.net
Accept: */*
Content-Length: 3
Content-Type: application/x-www-form-urlencoded
a=1
Response:
HTTP/1.0 405
date: Thu, 04 Oct 2012 21:53:32 GMT
server: CoralWebPrx/0.1.20 (See http://coralcdn.org/)
content-type: text/html
connection: close
Which seems to be normal because it acts as a CDN for any website, no mater if the website is ok or not.
Since a POST request means you want to submit something to the website, to create, update, or what ever, CoralCDN can't handle it because it can't submit something to an other website on its behalf. It could be a security issue. Nobody can POST what ever they want to a website anonymously...
If you want to send a parameter to the request, put them in the url (GET request are OK) but there won't be any solution to send a POST request to CoralCDN, even using cURL.
Error 405 means " This request method is not allow ."
You can change the request method from POST to GET . It may works.
<?php
$url = 'http://www.stackoverflow.com.nyud.net/';
$options = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n"
)
);
$context = stream_context_create($options);
echo file_get_contents($url,false,$context); //works
I am trying to execute a curl command that I can execute successfully in the terminal but it fails in PHP script with the following error:
HTTP/1.1 401 Unauthorized Access-Control-Allow-Origin: * Access-Control-Request-Method: * Cache-Control: no-cache Content-Type: application/json; charset=utf-8 Date: Mon, 06 Feb 2012 00:38:56 GMT Server: nginx/1.0.4 Set-Cookie: _parse_session=XXXXXX; domain=.parse.com; path=/; expires=Sun, 06-Feb-2022 00:38:56 GMT; HttpOnly Status: 401 Unauthorized WWW-Authenticate: Basic realm="Parse" X-Runtime: 0.002486 X-UA-Compatible: IE=Edge,chrome=1 Content-Length: 24 Connection: keep-alive {"error":"unauthorized"}
This is the command executed in terminal that executes successfully:
curl -H "Accept: application/json" -H "X-Parse-Application-Id: XXXXXXX" -H "X-Parse-REST-API-Key: XXXXXXXXX" -X GET "https://api.parse.com/1/classes/XXXXXX"
This is the PHP code:
$fields = array('Accept: '=>'application/json',
'X-Parse-Application-Id:' => 'XXXXX',
'X-Parse-REST-API-Key:' => 'XXXXX');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.parse.com/1/classes/XXXX');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_HEADER, $fields);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt(CURLOPT_USERPWD, 'XXXXXX');
$result = curl_exec($ch);
curl_close($ch);
The interesting note is that when executed in the terminal authentication info is not required.
Help will be appreciated.
Thanks
Your header fields shouldn't have the : in the array defnition:
'X-Parse-REST-API-Key:' => 'XXXXX');
^---remove these
That makes the : part of the field name, so you're actually sending:
X-Parse-REST-API-Key:: XXXXX
^^---note the doubled colons
Your header is set using CURLOPT_HTTPHEADER, not CURLOPT_HEADER which expects a boolean value