How do I submit Data as Form-Data with cUrl - php

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
]);

Related

CURLOPT_POST or CURLOPT_POSTFIELDS to send data

Should I use CURLOPT_POST or CURLOPT_POSTFIELDS to send data to server?
$curl = curl_init();
//this?
$post_data = array();
$post_data[] = "name: $name";
$post_data[] = "token: $token";
curl_setopt($curl, CURLOPT_HTTPHEADER, $post_data);
//or this?
$post_data = array();
$post_data['name'] = $name;
$post_data['token'] = $token;
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_CAINFO, "...");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_URL, $url );
$response = curl_exec($curl);
if (curl_errno($curl)) {
return(curl_errno($curl));
} else{
return $response;
}
curl_close($curl);

JSON raw POST cURL request not working

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);
?>
);

Content-Type application/json doesn't return POST fields

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.

Unexpected token error when integrating godaddy domain API

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 }'

How do you Upload Image to parse.com usint REST API + PHP?

Currently using this, but file that is stored is empty, so I supose no data is going through.
$post = array("file"=>'#'.$_FILES['uploadfile']['tmp_name']);
$ch = curl_init();
curl_setopt($ch,CURLOPT_HTTPHEADER,array('X-Parse-Application-Id : XXXXXXXXXXXXXX','X-Parse-REST-API-Key: XXXXXXXXx', 'Content-type: image/jpeg'));
curl_setopt($ch, CURLOPT_URL, 'https://api.parse.com/1/files/'.$_FILES['uploadfile']['name']);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
$result=curl_exec ($ch);
curl_close ($ch);
echo $result;
Got it working. You need to send the binary data as the post field. duh.
Before this you should probably create some restrictions (file size, type etc)
$post = file_get_contents($_FILES['uploadfile']['tmp_name']);
$ch = curl_init();
curl_setopt($ch,CURLOPT_HTTPHEADER,array('X-Parse-Application-Id : XXXXXXXX','X-Parse-REST-API-Key: XXXXXXXXXXXXXXX', 'Content-type: image/jpeg'));
curl_setopt($ch, CURLOPT_URL, 'https://api.parse.com/1/files/'.$_FILES['uploadfile']['name']);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result=curl_exec ($ch);
curl_close ($ch);
echo $result;
Use this work perfectly same way as we need.....
function sendImageToParse($url, $filename) {
$this->layout = FALSE;
$this->autoRender = false;
$headers = array(
'X-Parse-Application-Id:' . $this->APPLICATION_ID,
'X-Parse-REST-API-Key:' . $this->REST_API_KEY,
'Content-Type: image/jpeg'
);
$data = file_get_contents($url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, 'https://api.parse.com/1/files/' . $filename);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
function add() {
if ($this->request->is('post')) {
$giftUniqueId = time();
$urlFileImage = $allData['Coupon']['imageLink'];
//Eg : "http://localhost/favrapp/img/couponsLogo/gap.jpg";
$filename = $allData['Coupon']['imageName']; //Eg : "gap.jpg";
$responseFile = $this->sendImageToParse($urlFileImage, $filename);
$arrayInt = json_decode($responseFile);
if (!empty($arrayInt)) {
$name = $arrayInt->name;
$url = $arrayInt->url;
} else {
$name = '';
$url = '';
}
//Save to table by creating object
$url = 'https://api.parse.com/1/classes/giftcard';
$data = array('name' => $allData['Coupon']['name'],
'couponType' => $allData['Coupon']['coupontype'],
'giftcardTypeid' => $allData['Coupon']['giftcardTypeid'], 'giftcardcheckid' => $giftUniqueId,
'logo' => array('name' => $name, '__type' => 'File', 'url' => $url),
);
$_data = json_encode($data);
$headers = array(
'X-Parse-Application-Id: ' . $this->APPLICATION_ID,
'X-Parse-REST-API-Key: ' . $this->REST_API_KEY,
'Content-Type: application/json',
'Content-Length: ' . strlen($_data),
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $_data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_exec($curl);
curl_close($curl);
}
}

Categories