FiWare WP-OAuth user authentication PHP CURL - php

I am using KeyRock from FiWare.org. Previously everything seems to work find but now the rest server after user authorization send me the following response. I have looked at the server response and it seems to be a NULL/Empty response. But on the NodeJS the request is processed seamlessly. I am using wp-oauth plugin and have modified it to work with FiWare. I am sending this request via PHP CURL.
{
"error": {
"message": "Expecting to find application/json in Content-Type header - the server could not comply with the request since it is either malformed or otherwise incorrect. The client is assumed to be in error.",
"code": 400,
"title": "Bad Request"
}
}
Following is the CURL request I am using:
$url = URL_TOKEN . $url_params;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_USERPWD, CLIENT_ID . ":" . CLIENT_SECRET);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, (get_option('wpoa_http_util_verify_ssl') == 1 ? 1 : 0));
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, (get_option('wpoa_http_util_verify_ssl') == 1 ? 2 : 0));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);

have you tried to add application/json in Content-Type header?
BR

Related

Can't get the authorization code of Quickbooks API through curl but it works through the url

I just want to get the authorization code to obtain the access token to make API requests, I did the step manaully through the pasting the link in the browser and it works.
but through Curl PHP doesn't work I get empty string of size 1400 char.
this's the sample on Quickbooks API doc
curl -X POST 'https://appcenter.intuit.com/connect/oauth2?
client_id=Q3ylJatCvnkYqVKLmkxxxxxxxxxxxxxxxkYB36b5mws7HkKUEv9aI&response_type=code&
scope=com.intuit.quickbooks.accounting&
redirect_uri=https://www.mydemoapp.com/oauth-redirect&
state=security_token%3D138r5719ru3e1%26url%3Dhttps://www.mydemoapp.com/oauth-redirect'
Quickbooks API Auth code ref
I Turned to Curl PHP with replacing my credentials, when I visit the link I get the auth code and I could obtian the access token but can't do it in the back-end
the dir of the page that I want to get the auth code in is https://roifelawgroup.com/clientform/login/info_submit.php
but the redirect link just : https://roifelawgroup.com/ with login creds is this could be the problem?
However I tried with the same page redirect link didn't work
function authcode(){
$url = "https://appcenter.intuit.com/connect/oauth2?client_id=ABfQBA8VYPPUETO5isi2v8p39lQwEsw1ozL7NhZxxxxxx&response_type=code&scope=com.intuit.quickbooks.payment&redirect_uri=https://roifelawgroup.com&state=security_token%3D138r5719ru3e1%26url%3Dhttps://roifelawgroup.com";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // if removed this line I get 301 permanently moved
$headers = array(
"Content-Length: 0",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);
}

What's wrong with this PHP CURL script (integrating Foursquare API)?

I am trying to integrate Foursquare API to my website, here is the code:
$curl = curl_init('https://api.foursquare.com/v2/photos/add?v=20181008&oauth_token='.$token.'&photo='.$args['img'].'&venueId='.$title);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
curl_close($curl);
// Decode the response
$data = json_decode($data);
// Verify if the post was published
if ( #$data->meta->code == 200) {
return true;
} else {
return false;
}
But when I run this script on my website and try to post image, it is showing
An error occurred while processing your request
Remove the lines
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));
and try a simple
curl_setopt($curl, CURLOPT_POST, false);
You are sending the Request with GET Arguments
This could have multiple reasons.
I can't tell from the error message where exactly it triggers but when the curl fails, you should first investigate if there were any issues with the curl request itself.
Use curl_error for this: http://php.net/manual/de/function.curl-error.php
And are you using the API-Call the correct way?
In the docs POST https://api.foursquare.com/v2/photos/add
theres no $photo param at all.
Also when using POST you should add the parameters via
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
and not as GET-Parameters via URL.

Sending POST via curl to the server doesn't work

I am trying to send a POST request to the server (JBOSS) which is running on localhost:9090 as shown below but it doesn't seem to be working. I mean, I am not seeing my webservice getting executed. Am I doing everything right below?
Since $_POST["mydata"] contains the following :
[{ "name": "FirstName", "value": "Mickey" }, { "name": "LastName", "value": "Mouse" }]
which is the result of running JSON.stringify() on the javascript object (using serializeArray()), I am not sending application/json related thing in the header.
$urlTest = 'http://localhost:9090/JAXRS_POST_Request/rest/Request/insertDataToDB';
$rCURL = curl_init();
curl_setopt($rCURL, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($rCURL, CURLOPT_POSTFIELDS, $_POST["myData"]);
curl_setopt($rCURL, CURLOPT_URL, $urlTest);
curl_setopt($rCURL, CURLOPT_HEADER, 0);
curl_setopt($rCURL, CURLOPT_RETURNTRANSFER, 1);
$response_post = curl_exec($rCURL);
curl_close($rCURL);
But if I have to manually test the webservice using a client like POSTMAN, I have to specify under headers as Content-Type : application/json and send it like this through the Body and it works perfectly fine there :
But while sending the request via curl, since JSON.stringify() converts the javascript object to string to send it to the server, I am confused whether I need to use header thing in my POST request or not.
P.S. A simple curl based GET request has worked fine for me so my application is successfully contacting the server.
Try this:
$urlTest = 'http://localhost:9090/JAXRS_POST_Request/rest/Request/insertDataToDB';
$data = 'FirstName=Mickey&LastName=Mouse';
$rCURL = curl_init();
curl_setopt($rCurl, CURLOPT_POST, true);//
curl_setopt($rCURL, CURLOPT_POSTFIELDS, $data);
curl_setopt($rCURL, CURLOPT_URL, $urlTest);
curl_setopt($rCURL, CURLOPT_HEADER, 0);
curl_setopt($rCurl, CURLOPT_RETURNTRANSFER, true);
if(curl_exec($rCurl) === false){
echo 'Curl error: ' . curl_error($rCurl);
}else{
$response_post = curl_exec($rCURL);
}
curl_close($rCurl);

Displaying curl data, blank page

I'm trying to download json data using curl in PHP from companies house API.
Here is the example they provide that I'm trying to use:
https://developer.companieshouse.gov.uk/api/docs/company/company_number/readCompanyProfile.html#here
The example:
curl -uYOUR_APIKEY_FOLLOWED_BY_A_COLON: https://api.companieshouse.gov.uk/company/{company_number}
My code
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://api.companieshouse.gov.uk/company/01000000");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'curl -u8yUXrestOfmyApiKey:'
));
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($curl);
if(false == $result){
echo curl_error($curl);
}
curl_close($curl);
print($result);
?>
Using header:
'-u my_api_key:8yUXGgcHznjaNWnKpuKIJ7yv7HDvU_slH273GuF1'
I get 400 - bad request and with header in my code above I get nothing no errors blank page no data.
PS. There is a company with number 01000000 I've checked
You shuld add CURLOPT_USERPWD
curl_setopt($curl, CURLOPT_USERPWD, "YOUR API KEY"); #Add your api key

CURL,JSON and PHP

I am working to connect with an API using curl and PHP
I have this CURL statement
curl -i --user api:YXBpOmFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6MDEyMzQ1 --data-binary #test.png https://api.blahblah.com/blah
where YXBpOmFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6MDEyMzQ1 is the api key and #test.png is the image file I need to transfer to the API for upload. What I don't get is how to write the curl for the link above
and I also have this Example Statement as given by the site for developers(looks a JSON)
POST /blah HTTP/1.1
Host: api.blahblah.com
Authorization: Basic YXBpOmFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6MDEyMzQ1
Till now, I have written this up,
$url = 'https://api.tinypng.com/shrink';
$image = 'bf4lwp2.png';
$key = 'YXBpOmFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6MDEyMzQ1';
$jsonscript = array(
'Host' => 'api.tinypng.com',
'Authorization' => $key );
$json_string = json_encode($jsonscript);
$ch = curl_init($url);
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'api:'.$key);
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FILE, $image);
// Execute
$result=curl_exec($ch);
Also, if my request is a success, I get this...
HTTP/1.1 201 Created
Location: https://api.blahblah.com/blahblah.png
Content-Type: application/json; charset=utf-8
{
"input": {
"size": 87654
},
"output": {
"size": 102020,
"ratio": 0.236
}
}
I know I have to use file_get_contents() to get all that, but I don't know what URL to put in that function.
To get data from json first you can use json decode and than use foreach loop to
parse those data into variables

Categories