any help is very appreciated, thanks in advance.
my storeCallback.php file which i have hosted at: http://xxxxx/storeCallback.php
<?php
die(var_dump($_REQUEST));
and i am creating a post json raw request as:
1.
$url = "http://xxxxx/storeCallback.php/storeCallback.php";
$body =['foo' => 'bar'];
$body_string = json_encode($body);
$header = array(
'Accept: application/json',
'Content-Type: application/json',
'Content-Length: '.strlen($body_string),
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $body_string);
$json_response = curl_exec($curl);
$response = json_decode($json_response, true);
die(var_dump($response));
2.
$url = "http://xxxxx/storeCallback.php/storeCallback.php";
$body =['foo' => 'bar'];
$body_string = json_encode($body);
$header = array(
'Accept: application/json',
'Content-Type: application/json',
'Content-Length: '.strlen($body_string),
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $body_string);
$json_response = curl_exec($curl);
$response = json_decode($json_response, true);
die(var_dump($response));
3.
<?php
require 'vendor/autoload.php';
use GuzzleHttp\Client;
$client = new Client();
$response = $client->post('http://xxxxx/storeCallback.php/storeCallback.php', [
GuzzleHttp\RequestOptions::JSON => ['for' => "bar"],
]);
echo $response->getBody()->getContents();
Nothing is working, in all cases response is [] (empty array)
but sending request like http://xxxxx/storeCallback.php/storeCallback.php?foo=bar is working
There are two problems with your code, first of all the storeCallback.php file does not provide valid JSON output, therefore you cannot parse it as JSON when you try to retrieve it using cURL.
The proper version is the following:
storeCallback.php
<?php
die(json_encode($_REQUEST));
?>
Furthermore, you should be making the cURL request as follows:
<?php
$url = "http://pc.medimetry.in/storeCallback.php";
$body = ['foo' => 'bar'];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
$json_response = curl_exec($ch);
curl_close($ch); //important, always close cURL connections.
$parsed_response = json_decode($json_response);
var_dump($parsed_response);
?>
);
Related
I want to make a simple call to an API from PHP. It's working fine directly in CURL but not in PHP 7.3.
My real token was replace by TOKEN
CURL:
curl -X POST -H 'Authorization: Bearer TOKEN' -H "Content-Type: application/json" --data-binary '{"query":"{cameraList{name}}"}' https://cloud.camstreamer.com/api/graphql.php
PHP:
<?php
$url = "https://cloud.camstreamer.com/api/graphql.php";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);;
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array();
$headers["Authorization"] = "Bearer TOKEN";
$headers["Content-Type"] = "application/json";
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$data = '{"query": "{cameraList{name}}"}';
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
//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);
exit;
You can use
$info = curl_getinfo($ch);
$error = curl_error($ch);
to see what happened.
The content type was not sent correctly.
I changed this:
$headers = array();
$headers["Authorization"] = 'Bearer TOKEN';
$headers["Content-Type"] = "application/json";
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
To this:
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer TOKEN'
));
I have to send Data to an Api via PHP cUrl and then save the Token I get as a response.
In postman in works when I send the data as Form-data, but not if I send it as raw body. I now try to send the data via cUrl but I get the same response I get when I use postman with raw-body. How do I post it as form data?
Thanks
I tried those:
try:
$body = array();
$body['userEmail'] = $email;
$body['userPassword'] = $password;
$body['clientId'] = 'Kursapp';
$data_string = json_encode($body);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Accept: application/json;charset=UTF-8',
'Content-Type: application/json;charset=UTF-8'
));
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
$response = curl_exec($curl);
curl_close ($curl);
try:
$body = array();
$body['userEmail'] = $email;
$body['userPassword'] = $password;
$body['clientId'] = 'Kursapp';
$data_string = http_build_query($body);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Accept: application/json;charset=UTF-8',
'Content-Type: application/json;charset=UTF-8'
));
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
$response = curl_exec($curl);
curl_close ($curl);
try
$body = array();
$body['userEmail'] = $email;
$body['userPassword'] = $password;
$body['clientId'] = 'Kursapp';
$post_str ='';
foreach ($body as $key=>$value){
$post_str .= $key.'='.urldecode($value).'&';
}
$post_str = substr($post_str, 0, -1);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Accept: application/json;charset=UTF-8',
'Content-Type: application/json;charset=UTF-8'
));
curl_setopt($curl, CURLOPT_POSTFIELDS, $postr);
$response = curl_exec($curl);
curl_close ($curl);
try with guzzle
$client = new Client();
$response = $client->request('POST', $url, [
'headers' => [
'Accept' => 'application/json;charset=UTF-8',
'Content-Type' => 'application/json;charset=UTF-8'
],
'form_params' => $body
]);
I'm making cURL request to file on my server And it's printing empty array, unless I delete Content-Type: application/json header from request. Why it occure? It should expect json format in return....
$o = [ 'key' => 'value123' ];
$headers[] = 'Content-Type: application/json';
$headers[] = 'Moj-pierwszy-header: prosty/do/zapamietania';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $path.'test.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $o);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
echo( $output );
and the test.php
echo json_encode( $_POST );
Anybody? Any ideas?
Please refer the below code snippet:
$o = json_encode([ 'key' => 'value123' ]);
$headers[] = 'Content-Type: application/json';
$headers[] = 'Moj-pierwszy-header: prosty/do/zapamietania';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $path.'test.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $o);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
echo( $output );
and for output, instead of $_POST use below code snippet:
$post = json_decode(file_get_contents('php://input'));
Hope, it'll work for you.
Error
{"message":"Unexpected token '","body":"{'type':'A','name':'tarunDhiman','data':'166.62.81.221','ttl':3600}"}
Code
$data = "{'type':'A','name':'tarunDhiman','data':'166.62.81.221','ttl':3600}";
$url = "https://api.godaddy.com/v1/domains/{domain}/records";
$headers = array(
'Content-Type: application/json',
'Accept : application/json',
'Authorization : sso-key {key}:{token}' );
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
curl_close($curl);
print $response ;
exit;
The issue is solved by the help of #Slaiv206 And below the working code.
$data = '[{ "type":"A", "name":"tarunDhiman", "data":"255.255.255.0", "ttl":3600 }]';
$url = "https://api.godaddy.com/v1/domains/{domain}/records";
$headers = array(
'Content-Type: application/json',
'Accept : application/json',
'Authorization : sso-key {key}:{secret}' );
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
curl_close($curl);
print $response ;
exit;
I think the header Authorization string is:
Authorization: sso-key {KEY}:{SECRET}
and not:
Authorization : sso-key {key}:{token}
and in the json string use double quotes instead single quotes:
'{ "type":"A", "name":"tarunDhiman", "data":"166.62.81.221", "ttl":3600 }'
This part:
$response_2 = json_decode($response_2, true);
...of the code below is echoed literally as "Array" in the browser. If I remove the part, the full $response_2 is echoed in browser in JSON-format just like in this example: https://developer.spotify.com/web-api/get-list-users-playlists/
How come?
<?php
$url = 'https://accounts.spotify.com/api/token';
$method = 'POST';
$credentials = "hidden:hidden";
$headers = array(
"Accept: */*",
"Content-Type: application/x-www-form-urlencoded",
"Authorization: Basic " . base64_encode($credentials));
$data = 'grant_type=client_credentials';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response, true);
$token = $response['access_token'];
echo "My token is: " . $token;
$headers_2 = array(
"Accept: */*",
"Content-Type: application/x-www-form-urlencoded",
('Authorization: Bearer ' . $token));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://api.spotify.com/v1/users/wizzler/playlists');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_2);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response_2 = curl_exec($ch);
curl_close($ch);
$response_2 = json_decode($response_2, true);
echo $response_2;
?>
When you use echo on an array, it just prints out the literal string Array. This is just a quirk of PHP.
If you want to print out the contents of the array, you can use print_r() or var_dump().
However it seems like what you actually want to do is print the JSON, which is the string. $response_2 is already a string, so print it out.