I am creating a script that passes on some json formatted data. I'm having a issue encoding it correctly. Below is my code:
$data = array("email" => "theemail#email.com");
$data_string = json_encode(array('profiles' => $data));
$ch = curl_init('https://theurlisHere');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
echo $result;
I need my json to look like this:
"profile": [
{
"email": "theemail#email.com"
}
]
I can't figure out how to get the 'profile' object in to the array.
Thanks in advance.
You can use
json_encode(array('profile' => array($data)));
Related
$data = array(
"data1" => "1000",
);
$data_string = json_encode($data);
$ch = curl_init("http://www.example.com/api/info/getPlan");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json",
"Content-Length: " . strlen($data_string))
);
$result = curl_exec($ch);
https://i.stack.imgur.com/B9fKn.png
I'm trying to make a POST request from the portal with the following code. And got above error in browser. I'm using PHP 7.4,Apache 2.4.6 version.
I see some answer say to use CURLOPT_FOLLOWLOCATION or cookies and I try it but I cannot figure out how to do. Please some help is really appreciated.
Yes, using CURLOPT_FOLLOWLOCATION is the correct answer
follow this example to see if it work for you
$cookie_file_path = "/toyourfile";
$data = array(
"data1" => "1000",
);
$data_string = json_encode($data);
$ch = curl_init("http://www.example.com/api/info/getPlan");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
//curl_setopt($ch, CURLOPT_VERBOSE, true); // if you run on commandline this will print out more information for you
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json",
"Content-Length: " . strlen($data_string))
);
$result = curl_exec($ch);
I am trying to consume a service that I exposed from a project that I have in c #, the problem is that when consuming the service from php I get the following error.
{"error":"unsupported_grant_type"}
Code:
function __construct() {
# data needs to be POSTed to the Play url as JSON.
# (some code from http://www.lornajane.net/posts/2011/posting-json-data-with-php-curl)
$data = array("grant_type" => "password", "username" => "Administrador", "password" => "xxxx");
$data_string = json_encode($data);
print_r($data_string);
$ch = curl_init('https://integraciones.intergrupo.com/rest/oauth/token');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded')
);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
echo $result; // Outputs the JSON decoded data
}
You are sending a header 'Content-Type: application/x-www-form-urlencoded'.
With that you shouldn't json_encode $data, but just add the array to POSTFIELDS:
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
OR change the header to 'Content-Type: application/json'
CURLOPT_POSTFIELDS does not accept json_encode, but if I directly write the json in the CURLOPT_POSTFIELDS if it is entered
This returns an error
{"error":"bad_request","reason":"Request body must be a JSON object"}
$bd = "fiscont_db_catalogo_cuentas";
$ch = curl_init();
$document ='{"docs":[{"key":"baz","name":"bazzel"},{"key":"bar","name":"barry"}]}';
$json = json_encode($document);
echo $json;
curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1:5984/'.$bd.'/_bulk_docs');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); /* or PUT */
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-type: application/json',
'Accept: */*'
));
curl_setopt($ch, CURLOPT_USERPWD, 'root:addc1243c');
$response = curl_exec($ch);
echo $response;
curl_close($ch);
but this is returning :
[{"ok":true,"id":"8b3c672ffd4b8dcd7da313e9e9011243","rev":"1-f5f3f3e496c72307975a69c73fd53d42"},{"ok":true,"id":"8b3c672ffd4b8dcd7da313e9e9011c5a","rev":"1-8ad0e70d5e6edd474ec190eac2376bde"}]
$bd = "fiscont_db_catalogo_cuentas";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1:5984/'.$bd.'/_bulk_docs');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); /* or PUT */
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"docs\":[{\"key\":\"baz\",\"name\":\"bazzel\"},{\"key\":\"bar\",\"name\":\"barry\"}]}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-type: application/json',
'Accept: */*'
));
curl_setopt($ch, CURLOPT_USERPWD, 'root:addc1243c');
$response = curl_exec($ch);
echo $response;
curl_close($ch);
json_encode takes an array or an object as a parameter. In your example, you encode a JSON string to JSON which doesn't make sens.
If you replace your line by this:
$document =['docs' => [['key'=>'baz','name'=>'bazzel'],['key'=>'bar','name'=>'barry']];
It will correctly convert your PHP associative array to JSON.
/Unable to Send Multidimensional Json array through php cURL .
so in the below code i send it as array of Objects which will be difficult to retrieve in python/
$_api_url="http://example.com" ;
$params = http_build_query(array('data_Details' => json_encode($request)));
//initialize and setup the curl handler
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $_api_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
//execute request
$result = curl_exec($ch);
//close connection
curl_close($ch);
Try looking at the following example: POSTing JSON Data With PHP cURL
Useful excerpt:
$data = array("name" => "Hagrid", "age" => "36");
$data_string = json_encode($data);
$ch = curl_init('http://api.local/rest/users');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
$data_string = json_encode($request);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$request should be associated array format.
I'm trying to set up account creation via a payment form on my website using ZenDesk's API. The example code they give is:
curl -v -u {email_address}:{password} https://{subdomain}.zendesk.com/api/v2/users.json \
-H "Content-Type: application/json" -X POST -d '{"user": {"name": "Roger Wilco", "email": "roge#example.org"}}'
Since I need to include PHP variables, I'm trying to use this:
$data = array("name" => $entry["1"], "email" => $entry["3"], "role" => "end-user");
$data_string = json_encode($data);
$ch = curl_init('https://xxxx.zendesk.com/api/v2/users.json');
curl_setopt($ch, CURLOPT_USERPWD, "xxxx#example.com:xxxx");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
However, it's not working. Is my code correct in terms of duplicating the function of the first snippet?
I found another example of ZenDesk's API and was able to come up with this:
<?PHP
define("ZDAPIKEY", "SECRETKEYGOESHERE");
define("ZDUSER", "me#mysite.com");
define("ZDURL", "https://mysite.zendesk.com/api/v2");
/* Note: do not put a trailing slash at the end of v2 */
function curlWrap($url, $json, $action)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10 );
curl_setopt($ch, CURLOPT_URL, ZDURL.$url);
curl_setopt($ch, CURLOPT_USERPWD, ZDUSER."/token:".ZDAPIKEY);
switch($action){
case "POST":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
break;
case "GET":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
break;
case "PUT":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
default:
break;
}
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
curl_close($ch);
$decoded = json_decode($output);
return $decoded;
}
$arr = array("z_name"=>$namevariable,
"z_email"=>$emailvariable,
"z_role"=>"end_user",
"z_verified"=>"yes"
);
$create = json_encode(array('user' => array('name' => $arr['z_name'], 'email' => $arr['z_email'], 'role' => $arr['z_role'])), JSON_FORCE_OBJECT);
$data = curlWrap("/users.json", $create, "POST");
var_dump($data);
?>
It appears to be working on its own, so this answers the question as it exists here.
Thanks for your help everyone :)
I know the question is answered, but since I found it while having the same issue and since it did not solve my issue, I figured I'd post what I did. Hopefully, it can help someone else.
Here is the combination that worked for me in a case where I had to submit json data via PUT:
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 90);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json', 'Content-Length: ' . strlen($json), 'X-HTTP-Method-Override: PUT'));
Note that it does NOT require CURLOPT_CUSTOMREQUEST or CURLOPT_PUT since the X-HTTP-Method-Override: PUT parameter takes care of that.