I have tried the following code and receive a message saying my particular request is not supported, I cannot find any solutions that are not python for the v20 api. any help would be appreciated on what to use or where im going wrong
My error message is:
{"errorMessage":"Requested HTTP method is not supported for supplied
endpoint."}
<?php
$ch = curl_init();
$vars = "price=B&granularity=M5&count=20";
curl_setopt($ch, CURLOPT_URL,"https://api-fxpractice.oanda.com/v3/instruments/EUR_USD/candles");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$vars); //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = [
'Content-Type: application/json',
'Authorization: Bearer access-token',
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$server_output = curl_exec ($ch);
curl_close ($ch);
print $server_output ;
?>
My goal here is to successfully connect and get the response from the endpoint containing data. I have no python experience which is why im trying curl or even a javascript solution would work. Am i just not understanding this API at all?
Two things that stand out in the code are:
that there is no actual authorisation code. I don’t know if you have removed it for security while posting, or if you don’t realise you need to create a practice account and get an authorisation code for it? Both are free.
Your code curl_setopt($ch, CURLOPT_POST, 1); suggests you are trying a POST, whereas a GET is required for the \instruments endpoint.
But the simplest thing I can suggest that might help is to look at the following Github bash script that has taken OANDA's V1 API bash cURL example, and updated it for the v2 rest API.
https://github.com/p-burke/oanda-REST-v2-API-bash-script
Related
Here is my problem. I'm running a service on a remote machine working perfectly. The way to get the results from the machine is via api.
curl -X GET http://ip:777/api \
-d "r=request"
It works perfectly on the terminal. Moreover, it works perfectly, if the request query is short. But, it turns into a huge problem once, it passes some length(1800-2000 characters and I need 7k-8k chars).
However, I can't "transliterate" the curl code into PHP. If there is anyone with any idea how to do it please show me the way. As much as, I'm aware, this is a curl GET method with REQUEST BODY.
$long_query = "r=" . $request;
// set the api
curl_setopt($ch, CURLOPT_URL, 'http://ip:777/api');
// i want to get the return
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// 2min+ timeout as to make sure that I get a result
curl_setopt($ch, CURLOPT_TIMEOUT, 140);
// Set request method to GET by 0'ing the POST method
curl_setopt($ch, CURLOPT_POST, 0);
// Set query data here with CURLOPT_POSTFIELDS
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($long_query));
$content = curl_exec($ch);
curl_close($ch);
echo $content;
What am I doing wrong in here? If someone knows, please explain as if you are teaching a year old. Thanks in advance!
I think the following doc would help you understand how GET method works. This is from RFC 7231
A payload within a GET request message has no defined semantics; sending a payload body on a GET request might cause some existing implementations to reject the request.
For more details, please refer to this answer.
Alright, here we go with the proper answer.
on terminal,
curl -X GET http://ip:777/api \
-d "r=request"
works perfectly. However, the problem with converting that to php curl is quite troublesome while very easy at the same time.
I've read through every stack problem regarding this and no-one has provided a clear answer to the problem. I'm not sure the reason behind it but as a generous person I'll give out the code so that anyone in the future facing this rare problem will solve it out easily.
Long story short,
curl -X GET -d is the same as curl -X POST -H "X-HTTP-Method-Override: GET".
The actual request is POST but THE SERVER will consider it as a GET. This way you won't face the LONG URI problem.
$long_query = "r=" . $request;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"ip:777/api");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $long_query); //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = [
'X-HTTP-Method-Override: GET',
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$server_output = curl_exec ($ch);
curl_setopt($ch, CURLOPT_TIMEOUT, 140);
curl_close ($ch);
var_dump($server_output);
I've set the timeout to 140 as the query is long and it takes a bit of time for the server to go through it and respond (in my case its a json). Nevertheless, I've added var_dump so that anyone who uses it in the future might see if its a serialized array or whatever.
Good luck!
Seems like a simple task but despite having read their docs I can't figure it out. I've gotten a Storefront Access Token, as per https://help.shopify.com/en/api/storefront-api/getting-started. Then I've used it in the CURL request below. However this just returns: {"errors":"[API] Invalid API key or access token (unrecognized login or wrong password)"}
I found their documentation a bit confusing as they have quite a few different APIs, and whilst it seems that this Storefront Access Token should work for this particular API, perhaps it doesn't?
$url = "https://mysite.myshopify.com/admin/api/2019-07/products/count.json";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$headers = array(
'X-Shopify-Storefront-Access-Token: 2400d...........a999'
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
print_r( $result);
curl_close($ch);
If you are using the Stronfront API your request should point to
https://mysite.myshopify.com/api/2019-07/products/count.json
and not to
https://mysite.myshopify.com/admin/api/2019-07/products/count.json
In addition Storefront Api request are different from the standard ones, have that in mind.
Here's the situation. On one side, we have a server, with a RESTful service. One possible query to it is a POST query to create an object. When that is done, the server returns a 'Location' header, to indicate where information on the newly created object can be found.
However, said server is anal about having the correct Content-Type for each request. For instance, POST requires 'application/json', and GET requires this to be unset (make sense, since GET doesn't have a body).
To sum up, we have:
www.example.com/articles/ ; one can send a POST request with 'Content-Type: application/json', and server will return 'Location: www.example.com/articles/123' if 123 is the id of the new object ;
www.example.com/articles/123 ; one can send a GET request with no 'Content-Type' and server will return a description of the new article object.
On client side, we use PHP with cURL. We use the CURLOPT_FOLLOWLOCATIONsetting so we can read the description of the newly created object. Obviously, we also set 'Content-Type: application/json' for our POST request:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"name": "test"}');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_URL, "https://www.example.com/Articles/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
$result=curl_exec($ch);
var_dump($result);
curl_close ($ch);
?>
This is what we get:
string(101) "{ "errorNo": 415, "error": "Unsupported Media Type Content-Type should not be set in a GET request" }"
I looked at the log of the server, and indeed, 'Content-Type: application/json' is sent to GET www.example.com/articles/123.
Is this an expected behaviour?
If yes, what is then best approach:
remove the 'Content-Type' check on GET requests, server-side?
(sounds silly)
forget about CURLOPT_FOLLOWLOCATION, and make 2 clearly separated curl requests, so I have control over the headers? (but then what's the point of CURLOPT_FOLLOWLOCATION?)
something else?
For control and testing, I also use Postman, and I have no problem with it, it follows the location, doesn't send the 'Content-Type' on the GET part (apparently) after the redirection and so I don't have an error.
EDIT:
There seems to be nothing useful in the PHP doc. But I found something interesting in the command line man page:
https://curl.haxx.se/docs/manpage.html
It says:
"WARNING: headers set with this option will be set in all requests - even after redirects are followed, like when told with -L, --location."
So I guess it probably is the expected behaviour for PHP too. May someone suggest best practices then?
Have you tried using
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
to set the post type
we are currently trying to integrate into the Netsuite webservice, however we do not wish to use their PHP connector kit. we have already got a script we have developed using Coldfusion and cfhttp to post data into Netsuite without any issue.
Right now we currently have the scripts converted into PHP and are using the curl function to send data to Netsuite.
We can successfully create a new sales order record without any issue, however obtaining the return response message is proving to quite difficult as all we are getting is 302 redirects.
Im assuming that this may have something to do with login credentials for performing the collecting of the return object, however I have no idea on how to store this data in a cookie value so it can continue to be read across the system to system integration.
Below is our CURL code
$http_header = array(
'Content-Type: text/xml; charset="utf-8"',
'SOAPAction: getItemAvailability',
'Content-Length: ' . strlen($myXml),
'Accept: text/xml',
'Cache-Control: no-cache',
'Pragma: no-cache'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->wdsl);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $http_header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $myXml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
$returnObj = curl_exec($ch);
curl_close($ch);
we have tried various things but cannot seem to get it to work so we can obtain the return xml data, its creating the records with no issue what so ever.. Any suggestions would be greatly appreciated
I would recommend using a wrapper library instead of curl for NetSuite communication. I've done a lot of work with the ruby wrapper and it's worked great for me.
One of the reasons you can get a 302 is that account you are trying to access is in a different datacenter than the current WSDL you are pointing to. The standard WSDL domain is system.netsuite.com but many NetSuite installations are on system.na1.netsuite.com. The easiest way to check which datacenter you are on is to login to the GUI and look at the domain.
I've spent hours troubleshooting this utilizing PHP documentation, the API documentation, as well as other posts on stackoverflow, and am finally asking for help.
I am attempting to write an interface utilizing the new pbSmartConnections API: API Documentation
I have been having challenges with both fsockopen and cURL, however I seem to be able to get farther in the process utilizing cURL, so that's what I'm presenting here. Here's the challenge:
Per my understanding of the documentation, I should be passing the ApiKey as part of the header. When I do this, regardless of the different ways I've attempted to structure the code, I ALWAYS receive the following response:
{
"ErrorCode": 10,
"Message": "Unauthorized"
}
I'm hoping a fellow SO member can see something in my code below (please offer any criticisms and/or suggestions, too!):
(NOTE: The API key below IS valid. It's connected to an account with nothing of value in it, so feel free to use it in your testing)
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://rest.pbsyscontrol.com/v1/Ping");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type"=>"application/json", "Accept"=>"application/json", "ApiKey"=>"41460b3f-8f35-4878-b78d-49ca7f29c071"));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
?>
In case you are wondering, while I would like this to work as part of the header, I HAVE tried passing it as a part of the URL as well:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://rest.pbsyscontrol.com/v1/Ping?ApiKey=41460b3f-8f35-4878-b78d-49ca7f29c071");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type"=>"application/json", "Accept"=>"application/json", "ApiKey"=>"41460b3f-8f35-4878-b78d-49ca7f29c071"));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
?>
The PHP documentation says:
An array of HTTP header fields to set, in the format array('Content-type: text/plain', 'Content-length: 100')
You thus want to use the following line instead of the original:
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Accept: application/json", "ApiKey: 41460b3f-8f35-4878-b78d-49ca7f29c071"));
This does not however, solve the problem that the ApiKey probably isn't valid.
I finally heard back from support, and what they indicated was that I was using the incorrect url (although at this time, it IS the url in their API Documentation)
The URL in the API documentation was their STAGING, not PRODUCTION. Amazing what switching the URL to the correct one that they sent in their reply - rest.api.pbsmartconnections.com does for the connection. That one change and everything began working properly.
THANK YOU all who took a look, and to #mvdnes for the recommendations on how to set headers.