I am currently trying to use the Nest API but I can't get past the authorization part.
Here is the PHP code I use to call the API :
//access token url used for authorization
$url="https://api.home.nest.com/oauth2/access_token?code=".$_GET['pin']."&client_id=".$api_key."&client_secret=".$secret_key."&grant_type=authorization_code";
$ch = curl_init ($url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
//https
curl_setopt($ch, CURLOPT_SSLVERSION,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST,true);
//to have headers in response
curl_setopt($ch, CURLOPT_HEADER, 1);
$result = curl_exec ($ch);
if($result){
echo "Result : " .$result;
}else{
echo curl_error($ch);
}
And I get the following result for this call :
Result : HTTP/1.1 200 Connection established HTTP/1.1 400 BAD_REQUEST Content-Length: 0 Connection: Close
Is there other options I need to set in order to get it working?
EDIT : The generated url ($url) works fine with HTTPRequester (Firefox addon) so the problem comes most certainly from the curl request itself
Thanks in advance.
HTTP Status codes
400 - Bad request - typically this occurs when a bad parameter is
given to a method
So, try this:
echo $url;
exit;
before:
$ch = curl_init ($url);
and copy paste that url to your browser address bar. See where you're posting wrong parameter.
See, if that helps.
I think dev and inspect are running on same port. so remove the port and host for inspect on node by setting "inspect": false
angular.json
"serve": {
"builder": "#nrwl/node:execute",
"options": {
"buildTarget": "api:build",
"inspect": false
}
},
Related
I have a php curl script that returns the results of a get the run as a command from another process. The code is:
<?php
$arr = getopt("f:");
$url = $arr['f'];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo curl_error($ch);
} else {
echo $result;
}
curl_close($ch);
?>
When I do a api get request for a specific url with curl that gives a 400 error, curl_error($ch) is "The requested URL returned error: 400 Bad Request"
When I run the same request in postman, I get a json reply such as: {"result_ok":false,"code":400,"message":"Invalid Email: xxxxxxxxxx#ail.com (POST)"}.
How can I get the json returned in the curl request? If I echo the $result when there is an error condition, it is null.
From CURLOPT_FAILONERROR explained:
fail the request if the HTTP code returned is equal to or larger than 400. The default action would be to return the page normally, ignoring that code.
CURLOPT_FAILONERROR is false by default so either remove it or set it:
curl_setopt($ch, CURLOPT_FAILONERROR, false);
I'm trying to write a request with curl. I want to get the distance between two points. Therefore I'm getting the data of those points from my db to build the string. The final string looks like this when I echo it.
http://maps.googleapis.com/maps/api/distancematrix/json?origins=40215+Düsseldorf+Königsallee 59&destinations=40215+Düsseldorf&sensor=false
I also tried it with https instead of http but the result was the same.
As you can see, it returns a perfectly fine JSON-Response, but when I do this afterwards, I always get an error.
public function request($signedUrl) {
$ch = curl_init($signedUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$this->response = json_decode($data);
}
The $signedUrl is the requestUrl.
The error from Google I get, when I var_dump($data) is
400. That’s an error.
Your client has issued a malformed or illegal request. That’s all we know. "
When I var_dump the response, it just gives me null.
What could be the problem here and how could I fix it? I also tried to read it in with file_get_contents but without success
Works [Tested] . You got a bad request since the URL was not encoded.
<?php
$urlx='http://maps.googleapis.com/maps/api/distancematrix/json?origins=40215+D%C3%BCsseldor%20%E2%80%8Bf+K%C3%B6nigsallee59&destinations=40215+D%C3%BCsseldorf&sensor=false';
$ch = curl_init($urlx);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
var_dump(($data));
I have a problem in HTTP Authentication. I couldn't get the content of this url because it need http auth.
Code:
<?php
$url = "http://www.abcdefg.com/12345/";
$username = 'username';
$password = 'password';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$out = curl_exec($ch);
print "error:" . curl_error($ch) . "<br />";
print "output:" . $out . "<br /><br />";
curl_close($ch);
?>
The Problem is:
Instead of show the real content, it shows "302 Found, The document has moved here."
I've tried "http://username:password#www.abcdefg.com/12345/", doesn't work.
When accessing this url(1), a popup window ask for username and password. but the popup window is from another url(2)(a sso authentication server). if pass the authentication. then it gets back to url(1) again. at this time, I can access the content of this url(1).
I use firebug get the following message by accessing the url directly from browser:
Step 1
URL: GET http://www.abcdefg.com/12345/
Status: 302 Found
Protocol: http
Step 2
URL: GET https://www.ssoauth.com/obrareq.cgi?wh=xxx wu=xxx wo=xxx rh=http://www.abcdefg.com ru=/12345/
Status: 302 Redirect
Protocol: https
Step 3
URL: GET http://www.abcdefg.com/obrar.cgi?cookie=xxxxxxxxxxxxxxx
Status: 302 Found
Protocol: http
Step 4
URL: GET http://www.abcdefg.com/12345/
Status: 200 OK
Protocol: http
Then display the content…
Is this something to do with the cookie?
How can I use php curl to read the content?
You have to set:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
This will make cURL obey the 302 and generate an additional request.
I'm probably not supposed to use file_get_contents() What should I use? I'd like to keep it simple.
Warning: file_get_contents(http://en.wikipedia.org/w/api.php?action=query&titles=Your_Highness&prop=revisions&rvprop=content&rvsection=0): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden
The problem you are running into here is related to the MW API's User-Agent policy - you must supply a User-Agent header, and that header must supply some means of contacting you.
You can do this with file_get_contents() with a stream context:
$opts = array('http' =>
array(
'user_agent' => 'MyBot/1.0 (http://www.mysite.com/)'
)
);
$context = stream_context_create($opts);
$url = 'http://en.wikipedia.org/w/api.php?action=query&titles=Your_Highness&prop=revisions&rvprop=content&rvsection=0';
var_dump(file_get_contents($url, FALSE, $context));
Having said that, it might be considered more "standard" to use cURL, and this will certainly give you more control:
$url = 'http://en.wikipedia.org/w/api.php?action=query&titles=Your_Highness&prop=revisions&rvprop=content&rvsection=0';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, 'MyBot/1.0 (http://www.mysite.com/)');
$result = curl_exec($ch);
if (!$result) {
exit('cURL Error: '.curl_error($ch));
}
var_dump($result);
The error message you are really receiving is
Scripts should use an informative User-Agent string with contact information, or they may be IP-blocked without notice.
This means that you should provide additional details about yourself when using the API. Your usage of file_get_contents does send the required User-Agent.
Here is a working example in curl that identifies itself as a Test for this question:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://en.wikipedia.org/w/api.php?action=query&titles=Your_Highness&prop=revisions&rvprop=content&rvsection=0&format=xml");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, "Testing for http://stackoverflow.com/questions/8956331/how-to-get-results-from-the-wikipedia-api-with-php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
file_get_contents Should work.
file_get_contents('http://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles=New_York_Yankees&rvprop=timestamp|user|comment|content')
This was previously discussed on stackoverflow here
Also, some nice looking code samples here
They themselves say in their API documentation:
Use any programming language to make an HTTP GET request for that URL
You need to get the URL right, thefollowing worksfor me :
http://en.wikipedia.org/w/api.php?format=json&action=query&titles=Main%20Page&prop=revisions&rvprop=content
you are not specifying the output format as far as I can notice right now!
I have a frontend code
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
//curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($ch, CURLOPT_URL, $url);
//make the request
$responseJSON = curl_exec($ch);
$response_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($response_status == 200) { // success
// remove any "problematic" characters from the json string and then decode
if (debug) {
echo "----finish API of getAPI inside basic_function with status==200---";
echo "<br>";
echo "-------the json response is-------" ; //.$responseJSON;
var_dump($responseJSON);
//print_r($responseJSON);
echo "<br>";
}
return json_decode( preg_replace( '/[\x00-\x1F\x80-\xFF]/', '', $responseJSON ) );
}
and I have a backend code which executed when cURL fired its operation with its URL. The backend code would therefore activated. So, I know cURL is operating.
$output=array (
'status'=>'OK',
'data'=>'12345'
)
$output=json_encode($output)
echo $output;
and $output shown on browser as {"status":"OK","data":"12345"}
However, I gone back to the frontend code and did echo $responseJSON, I got nothing. I thought the output of {"status":"OK","data":"12345"} would gone to the $responseJSON. any idea?
Here's output on Browser, something is very odd! the response_status got 200 which is success even before the parsing of API by the backend code. I expect status =200 and json response after the {"status":"OK","data":"12345"}
=========================================================================================
inside the get API of the basic functions
-------url of cURL is -----http://localhost/test/api/session/login/?device_duid=website&UserName=joe&Password=1234&Submit=Submit
----finish API of getAPI inside basic_function with status==200---
-------the json response is-------string(1153)
"************inside Backend API.php******************
---command of api is--/session/login/
---first element of api is--UserName=joe
--second element of api is---Password=1234
---third element of api is----Submit=Submit
----fourth element of api is---
-------inside session login of api-------------
{"status":"OK","data":"12345"}
Have you tried with curl_setopt($ch, CURLOPT_TIMEOUT, 10); commented?
See what happends if you comment that line.
Also try with the a basic code, if that works, smthing you added later is wrong:
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, false);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
Try var_dump($responseJSON)
If it returns false try
curl_error ( $ch )
Returns a clear text error message for the last cURL operation.
Are you sure your $url is correct?