I try to send request from post type to server but it not success, It return me BadRequest with status 400,
My code:
<?php
$products = array('1'=>array('amount'=>'1','product_id'=>'11250'));
$data = array('id' => '67', 'shipping' => '61', 'payment'=> '2','products'=> $products);
$cert = base64_encode('myapp#myapp.co.il:1234abcd');
$url = "https://myapp.co.il/api/aaa";
$ch = curl_init($url);
$payload = json_encode($data);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"authorization: Basic ".$cert,
"Content-Type: application/json",
"cache-control: no-cache"
));
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);
?>
It return me so:
array (2) {["message"] => string (51) "Bad Request: Syntax error, distorted JSON" ["status"] => int (400)}
I see that you prepared variable $payload to send, but sent $postdata instead at curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata).
Related
I am using ci framework, and i try to create notification using
onesignal API. I have hosted the website using https. But why my JSON
received is always false using onesignal API, but if i test using
postman it runs fine without error.
This is my code
function send_message($id, $nama) {
$content = array(
"en" => $nama
);
$heading =array(
"en" => $id
);
$fields = array(
'app_id' => "2cd5ad24-a2d9-4e7d-ac66-7494cebd8e84",
'included_segments' => array(
'Active Users'
),
'contents' => $content,
'headings' => $heading,
'url' => "https://esop.matahariled.com"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Authorization: Basic MDQ0NjY0ZmYtZjQ2Yi00ODVmLTkzZjgtZmVkZDBkODk0MDFl'
));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_CAINFO, $_SERVER['DOCUMENT_ROOT']."/cacert.pem");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
I have try to create post curl json array multidimensional but the result is NULL
i dont know why is not working.
this is my code
$d = array('code' => 1111, 'number' => "08813812");
$array = array('content' => $d);
$result = json_encode($d);
// echo $result;
$data = array(
'content' => $d,
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost/receive-callback.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"cache-control: no-cache",
"postman-token: 701ab5b6-002e-a982-0cfd-c6eee34ae7e1",
"Content-Type: application/json"
));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
echo $response;
this is for receive the post
$d = $_POST['content'];
// file_put_contents('hehe.txt', $d);
print(json_encode($d, JSON_PRETTY_PRINT));
// $ho = json_decode($d);
// echo $d['code'];
in this case , i want the result in receiver is value from object array content
Change your code with the following:-
$d = array('code' => 1111, 'number' => "08813812");
$array = array('content' => $d);
$result = json_encode($d);
// echo $result;
$data = array(
'content' => $d
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost/stackoverflow/receive-callback.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"cache-control: no-cache",
"postman-token: 701ab5b6-002e-a982-0cfd-c6eee34ae7e1",
"Content-Type: application/json"
));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($ch);
echo $response;
To receive use the follwoing :-
$data = json_decode(file_get_contents('php://input'), true);
print_r($data);
I want to send JSON data to http://localhost:3000/certificates using curl in PHP
Here is what I do:
$url = 'http://localhost';
$ch = curl_init($url);
$jsonData = array(
'certno' => 'AB1234',
'issueDate' => 'asdasd',
'type' => 'asdasdasd'
);
$jsonDataEncoded = json_encode($jsonData);
curl_setopt($ch, CURLOPT_PORT, 3000);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
How to add /certificates ? I cannot simply put it in url http://localhost/certificates right?
how can I make an HTTP REQUEST in curl with json?
I need to put this in curl for php:
POST /api/ra/v1/ping HTTP/1.0
Host: app.kigo.net
Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=
Content-Type: application/json
{
"PING" : "PONG"
}
Thanks in advance!
EDIT
This is what I've tried, but it shows "Invalid Content-Type header.":
<?php
$url = 'https://app.kigo.net/api/ra/v1/ping';
$headers = array( 'Authorization' => 'Basic dXNlcm5hbWU6cGFzc3dvcmQ=', 'Content-Type' => 'application/json' );
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch,CURLOPT_POSTFIELDS, "PING=PONG");
$result = curl_exec($ch);
curl_close($ch);
print_r( $result );
?>
Your CURLOPT_HTTPHEADER array must be in the form array('Header1: value1', 'Header2: value2') and not array('Header1' => 'value1', 'Header2' => 'value2'), see examples in http://php.net/manual/en/function.curl-setopt.php
Also, if you post JSON data, you should probably json_encode() the data you are sending.
$headers = array(
'Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=',
'Content-Type: application/json'
);
$postData = json_encode(array(
'PING' => 'PONG'
));
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, 'https://app.kigo.net/api/ra/v1/ping');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
I'm trying to create a payment (Paypal)
My data provided by paypal : https://developer.paypal.com/webapps/developer/docs/api/
$data = '{All the data}';
cURL..
$ch = curl_init();
$headers = array(
'Content-type: application/JSON',
'Authorization: Bearer A101.YqvcjeJ6va_06Zd3ZQSkn8uNeH0Il1KJKCy72pc0ReSZ_n-cA9oEbVSArBc78F-e.iVCsmw63xoQHrfe7W1v_MYGHhUS',
);
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/payments/payment");
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response= curl_exec($ch);
var_dump($response);
$json = json_decode($result);
print_r($json);
curl_close($ch);
The response:
[name] => UNKNOWN_ERROR
[message] => An unknown error has occurred
[information_link] => https://developer.paypal.com/webapps/developer/docs/api/#UNKNOWN_ERROR
[debug_id] => 58039c1674622
So guys, what is wrong with my code?
Thanks for the help!