PHP Unable to Return curl response - php

I cannot return the response. I successfully tried this request with Postman. The result is in JSON format already, I want to return JSON. If I mess with the header, the error message returned by the api is successfully displayed. The whole point is simply to return the JSON-data, it is retrieved of an API, but the app that I want to display the data in does not support headers, so I'm trying to build an URL which displays the data without any headers needed.
EDIT Solved
Following Phil I've updated my code to:
$url = "https://api.com/data";
$headers = array(
"accept-encoding: gzip",
"cache-control: no-cache",
"connection: keep-alive",
"content-type: application/json",
"Authorization: Bearer superlongkey"
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_ENCODING, "gzip");
$resp = curl_exec($curl);
curl_close($curl);
$response->getBody()->write($resp);
return $response
Additionally, I had to include the line
curl_setopt($curl, CURLOPT_ENCODING, "gzip");
to decode the data from gzip
$url = "https://api.com/data";
$headers = array(
"accept-encoding: gzip",
"cache-control: no-cache",
"connection: keep-alive",
"content-type: application/json",
"Authorization: Bearer superlongkey"
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$resp = curl_exec($curl);
curl_close($curl);
$response->getBody()->write(json_encode($resp));
return $response
The error I get is
fwrite() expects parameter 2 to be string, boolean given
I've also tried to leave out the fwrite line, but then I get the following error:
Return value of Slim\Handlers\Strategies\RequestResponse::__invoke() must implement interface Psr\Http\Message\ResponseInterface, string returned

Related

True Caller Mobile Web SDK Integration

I am integrating Truecaller mobile web SDK in my CodeIgniter application for verification. I am successfully invoking the true caller for verification, but I am not getting the response at the endpoints. It is mentioned in the document that they post the response in a few milliseconds, but I am accessing that with the $_POST variable. Is it correct? Can anyone guide me in this, please?
if (isset($_POST["requestId"]) != 'null') {
$endpoints = $_POST["endpoint"];
log_message('error', $_POST["requestId"]);
$url = $endpoint;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$access_token = "Bearer ".$_POST["accessToken"];
$headers = array(
"Authorization: $access_token",
"Cache-Control: no-cache",
"Content-Type: application/json",
);
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);
}

Is there anything wrong with my request when CURL POST request returns error 55

I'm trying to make a POST request using curl in php and am getting curl error #55. Have been looking around and don't see much information on this error. I don't think my request is formed wrong.
$curl = curl_init();
$encoded = base64_encode("12345:12345");
curl_setopt($curl, CURLOPT_URL, "https://accounts.spotify.com/api/token");
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json\r\n",
"Accept: application/json",
'Authorization: Basic ' . $encoded,
"Accept: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, array(
"grant_type" => "refresh_token",
"refresh_token" => 'CxRmNSepUaA41Zs8xIN68fC2wYbRrsrQWKE9547fF6Q12LDOpr551xb95K62+EqnGq6glEHXF4R3qgtgCnOVpr9wFAaxta5/iBKzYBqk+2B442qgiUQu7GyAm+mD1ick3vGrdlZgEEpr/U9EcVVGqXf3XN1EhlZa/vYGgO2opkKedFgbN53Hdd+xQ=='
));
$server_output = curl_exec($curl);
$err = curl_errno($curl);
print("CURL error: ");
print($err);
print($server_output);

Using curl to post an array to the godaddy api

I am trying to post a bunch of domains to the godaddy api in order to get information about pricing and availability. However, whenever I try to use curl to execute this request, I am returned nothing. I double checked my key credentials and everything seems to be right on that end. I'm pretty confident the issue is in formatting the postfield, I just don't know how to do that... Thank you to whoever can help in advance!
$header = array(
'Authorization: sso-key ...'
);
$wordsArray = ['hello.com', "cheese.com", "bytheway.com"];
$url = "https://api.godaddy.com/v1/domains/available?checkType=FAST";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_POST, true); //Can be post, put, delete, etc.
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $wordsArray);
$result = curl_exec($ch);
$dn = json_decode($result, true);
print_r($dn);
There are two problems in your code:
Media type of sent data must be application/json (by default this is application/x-www-form-urlencoded), and your PHP app must accept application/json as well:
$headers = array(
"Authorization: sso-key --your-api-key--",
"Content-Type: application/json",
"Accept: application/json"
);
Post fields must be specified as JSON. To achieve this, use the json_encode function:
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($wordsArray));
Full PHP code is:
$headers = array(
"Authorization: sso-key --your-api-key--",
"Content-Type: application/json", // POST as JSON
"Accept: application/json" // Accept response as JSON
);
$wordsArray = ["hello.com", "cheese.com", "bytheway.com"];
$url = "https://api.godaddy.com/v1/domains/available?checkType=FAST";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($wordsArray));
$result = curl_exec($ch);
$dn = json_decode($result, true);
print_r($dn);

API requires a encoded API key, how do i send this with curl?

I am working with an api that requires an api key to be sent in basic auth of a curl call.
$json = "somedata";
$url = "http.server.com";
$key = "someKey";
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10 );
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, $key);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/2.0");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
curl_close($ch);
My response is:
"Server error","data":{"details":"Invalid API key.
I know the key itself is correct, It works when sending a curl request not using php.
I assume USERPWD is not the correct curlopt to use, which one is?
From docs:
A Base64 encoded string, generated from the combined username:password
sequence.
In our case, the API key is set as username, and password is set as an empty
string.
For example, if the API Key is equal toN8KzwcqVUxAI1RoPi5jyFJPkPlkDl9vF,
the Base64 encoding should be performed on the following string:
N8KzwcqVUxAI1RoPi5jyFJPkPlkDl9vF:
In this case, the content sent to
the authorization header is
Basic TjhLendjcVZVeEFJMVJvUGk1anlGSlBrUGxrRGw5dkY6.
API Docs
USERPWD is correct, but you likely need to send
$apiuser:$apikey
looks like now, you are just sending the key.
I adjusted how i sent the headers:
$headers = array(
"POST ".$url." HTTP/1.0",
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: \"run\"",
"Content-length: ".strlen($xml_data),
"Authorization: Basic " . $key
);
and added
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

PHP curl_setopt equivalent to curl -d

I got the following curl command to work on Linux:
curl -H "Content-Type:application/json" -H "Accept:application/json" -H "Authorization: Basic dGVsZXVuZzpuYWcweWEyMw==" -X PUT -d '{"requireJiraIssue": true, "requireMatchingAuthorEmail": "true"}' http://stash/rest/api/1.0/projects/TSD/repos/git-flow-release-test/settings/hooks/com.isroot.stash.plugin.yacc%3AyaccHook/enabled
However, when I tried to do this on PHP, the data is not being sent to the server properly, this is my set_opt commands:
$myURL = "http://stash/rest/api/1.0/projects/TSD/repos/git-flow-release-test/settings/hooks/com.isroot.stash.plugin.yacc:yaccHook/enabled";
$hookdata_yacc = array(
'requireJiraIssue' => true,
'requireMatchingAuthorEmail' => true
);
$data = json_encode($hookdata_yacc);
$headers = array(
"Content-Type: application/json",
"Accept: application/json",
"Authorization: Basic dGVmyPasswordEyMw==",
"Content-Length: " . strlen($data)
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $myURL);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_PUT, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
return (curl_exec($curl));
What did I miss?
You use CURL options improperly. CURLOPT_PUT is intended for sending files, it is not suited for your case. You have to use CURLOPT_CUSTOMREQUEST option and set it to "PUT", not "POST".
So the code should look like this:
$myURL = "http://stash/rest/api/1.0/projects/TSD/repos/git-flow-release-test/settings/hooks/com.isroot.stash.plugin.yacc:yaccHook/enabled";
$hookdata_yacc = array(
'requireJiraIssue' => true,
'requireMatchingAuthorEmail' => true
);
$data = json_encode($hookdata_yacc);
$headers = array(
"Content-Type: application/json",
"Accept: application/json",
"Authorization: Basic dGVmyPasswordEyMw==",
"Content-Length: " . strlen($data)
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $myURL);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
return (curl_exec($curl));

Categories