curl fail to set header application/json php - php

My goal is to connect a private web service on my job.
Im trying to connect but when I set Content-Type to application/json it fails and return null, someone can help?
This is my code:
$curl = curl_init();
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json;charset=UTF-8')
);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
$fp = fopen(WAREHOUSE . '/errorlogc.txt', 'w');
curl_setopt($curl, CURLOPT_STDERR, $fp);
$json_response = curl_exec($curl);
when I execute this code it fails and return null and no more info, even the log I set for it show's me nothing.

fixed! the problem was that before trying to send data to the server I have to set a cookie for the session, so I get the cookie from the header response on the connection and then send the data.
thanks for the help

Related

PHP set set --raw-data in put request

I need to send a PUT request with cUrl in PHP. However, I don't get the request body to work.
I need to send the following JSON as PUT request.
{
"orders": [
{
"order_id": "TBRQCH"
}
]
}
Here is what I tried:
$data['orders'][] = array("order_id" => $orderid);
$curl = curl_init();
curl_setopt($curl, CURLOPT_PUT, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "XXXX");
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
However, it does not work correctly because I get the message from the endpoint that orders is missing. When copy the encoded string to Postman and send the request manually to the API then it works fine.
What am I doing wrong?

error string(161) " Cannot POST /api/2/trading/balance " HitBTC Api PHP

My code is below doesn't work, I basically want to display the account balance, I am using var_dum($curl_response) to actually see where the code stopped, cuz if I dump the final result I get null.
<?php
$headers = array ( "Content-Type: application/json" );
$service_url = 'https://api.hitbtc.com/api/2/trading/balance';
$curl = curl_init($service_url);
curl_setopt ($curl, CURLOPT_HTTPHEADER, $headers );
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "xxxx:xxx"); //Your credentials goes here
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //IMP if the url has https and you don't want to verify source certificate
$curl_response = curl_exec($curl);
$response = json_decode($curl_response);
curl_close($curl);
var_dump($curl_response);
?>
Just for those who came here looking for an answer I fixed it myself and this is to help you out, this is what really works with HitBTC without the need to their SDK.
<?php
$ch = curl_init('https://api.hitbtc.com/api/2/trading/balance'); curl_setopt($ch, CURLOPT_USERPWD, 'xxxx:xxxxx'); // API AND KEY
$return = curl_exec($ch); curl_close($ch); print_r($return); ?>

php curl post a large json object

So, I have a php script which sends a large JSON object and this is the code I am using to do so.
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_VERBOSE, true);
$json_response = curl_exec($curl);
Now when the data is small, the request goes through smoothly. However, when the json object becomes huge it suddenly fails to send the data and instead is shows "Content-Length: 0" in the request header as if it did not include the object.
I am assuming there is a limit to how large the data could be. Is there a way to bypass that?
I have searched for similar issues and I found a solution for sending files. But the object i have here is a result set from the database and therefore I would prefer to keep the file approach as a last resort.
Thanks.
So basically you have the HTTPHEADER already but you're missing the Content-length one try it like so:
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-length:' . strlen($data)));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_VERBOSE, true);
$json_response = curl_exec($curl);

Consume API with php and cURL

Hi I'm trying to consume an API.
I need to add the API key in the header
API endpoint = http://overheid.io/api/kvk
if you want to test things, you can create an account here: https://overheid.io/auth/register
Documentation is in Dutch and can be found here: https://overheid.io/documentatie/kvk
This is what I came up with but do not get passed authentication.
<?php
$service_url = 'https://overheid.io/api/kvk';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HEADER => true);
curl_setopt($curl, CURLOPT_HTTPHEADER, "ovio-api-key:the_api_key");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$curl_response = curl_exec($curl);
curl_close($curl);
print $curl_response;
?>
Edit: I've replaced the initial code part, with the solutions, but still no success.
Can anyone point me in the right direction?
Thanks!
You can try to use the CURLOPT_USERPWD option to pass the Authentication header. You also seemed to have some syntax errors.
<?php
$service_url = 'https://overheid.io/api/kvk';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_USERPWD, "ovio-api-key:the_api_key");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$curl_response = curl_exec($curl);
curl_close($curl);
print $curl_response;
But, this API seem to use a custom header...so you can try the following if it the above does not work:
curl_setopt($curl, CURLOPT_HTTPHEADER, array("ovio-api-key: yourkey"));
UPDATE:
I used the following with an account I created on their site:
<?php
$service_url = 'https://overheid.io/api/kvk';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("ovio-api-key: key"));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$curl_response = curl_exec($curl);
curl_close($curl);
print $curl_response;
This worked for me when I replaced the "key" part with the 64 character key they provide.

Post status on Google Plus using PHP Curl

I have this code for logging into Google using Simple DOM Parser with curl. I've tried to post status on google plus using following code but unable to post on google plus
Any idea on how to solve this?
Here's my code for reference:
$clientlogin_url = "https://www.google.com/accounts/ClientLogin";
$clientlogin_post = array(
"accountType" => "HOSTED_OR_GOOGLE",
"Email" => "youremail#gmail.com",
"Passwd" => "yourpassword",
"service" => "writely",
"source" => "your application name"
);
// Initialize the curl object
$curl = curl_init($clientlogin_url);
// Set some options (some for SHTTP)
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $clientlogin_post);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// Execute
$response = curl_exec($curl);
// Get the Auth string and save it
preg_match("/Auth=([a-z0-9_-]+)/i", $response, $matches);
$auth = $matches[1];
$params['newcontent'] = "Post on Google Plus Test By Me";
$headers = array(
"Authorization: GoogleLogin auth=" . $auth,
"GData-Version: 3.0",
);
// Make the request
curl_setopt($curl, CURLOPT_URL, 'https://www.plus.google.com/');
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
$response = curl_exec($curl);
curl_close($curl);
Thanks For your help and co-operation
There is not a Google+ API to post statuses (unless you are a Google Apps user) and ClientLogin has been deprecated and will stop working soon. You're best bet is to look into using something like the share plugin.
In order not show the moved you have to set the agent header like firefox or something else
example
curl_setopt($curl, CURLOPT_URL, 'https://www.plus.google.com/');
curl_setopt($curl,"USER_AGENT","firefox my browser");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //Added here
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //Added here
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // Since you got 301 redirect
$response = curl_exec($curl);
var_dump($response);// Added here
curl_close($curl);`enter code here`
You need to add these two cURL params here too.
curl_setopt($curl, CURLOPT_URL, 'https://www.plus.google.com/');
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //Added here
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //Added here
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // Since you got 301 redirect
Also, you are not outputting any response on your code
$response = curl_exec($curl);
var_dump($response);// Added here
curl_close($curl);

Categories