I've been banging my head against the wall on this one. I feel like my problem is something really silly, but I just can't figure out what.
I have a proxy script, which is handling everything (POST data and uploads).
When I print_r($_FILES) on the back-end server, it's an empty array, and $_POST contains the file path.
$ch = curl_init();
$options = array(
CURLOPT_HEADER => 1,
CURLOPT_HTTPHEADER => array(
"Host: {$host}",
"Cache-Control: no-cache"
),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_URL => "http://{$this->config->ipAddress}{$url}",
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_COOKIE => $cookie_string,
CURLOPT_USERAGENT => $_SERVER["HTTP_USER_AGENT"]
);
if (! empty($_POST)) {
if (! empty($_FILES)) {
//$options[CURLOPT_HTTPHEADER][] = "Content-type: multipart/form-data";
$files = "";
foreach ($_FILES as $fid => $file) {
$files .= "{$fid}=#" . $file["tmp_name"] . ";type={$file["type"]};name={$file["name"]}&";
}
}
$postString = (! empty($files) ? $files : '') . http_build_query($_POST);
$options[CURLOPT_POST] = 1;
$options[CURLOPT_POSTFIELDS] = $postString;
}
curl_setopt_array($ch, $options);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
The reason I'm using http_build_query instead of just feeding an array is because we will encounter a multidimensional array.
Thanks!
I'm willing to bet your form enctype isn't "multipart/form-data" b/c your not passing the post string as an array.
http://php.net/manual/en/function.curl-setopt.php search for "CURLOPT_POSTFIELDS"
below is an excerpt from that page.
"If value is an array, the Content-Type header will be set to multipart/form-data. As of PHP 5.2.0, value must be an array if files are passed to this option with the # prefix."
You can find out what is going on with the curl by setting the curl option "curl_setopt($ch, CURLOPT_VERBOSE, TRUE); " and then "print_r( curl_getinfo($ch) );" after the "curl_exec($ch);" and it will output all the information about the request and response.
Related
Im trying to convert a post connection to the walmart api, from php to ruby, this is the php version
$client_id = $data['client_id'];
$client_secret = $data['client_secret'];
$url = "https://marketplace.walmartapis.com/v3/token";
$uniqid = uniqid();
$authorization_key = base64_encode($client_id.":".$client_secret);
$code = "";
$ch = curl_init();
$options = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 60,
CURLOPT_HEADER => false,
CURLOPT_POST =>1,
CURLOPT_POSTFIELDS => "grant_type=client_credentials",
CURLOPT_HTTPHEADER => array(
"WM_SVC.NAME: Walmart Marketplace",
"WM_QOS.CORRELATION_ID: $uniqid",
"Authorization: Basic $authorization_key",
"Accept: application/json",
"Content-Type: application/x-www-form-urlencoded",
),
);
curl_setopt_array($ch,$options);
$response = curl_exec($ch);
$code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
curl_close($ch);
and this is what i have so far:
url = "https://marketplace.walmartapis.com/v3/token/"
uniqid = "1234567890a1b"
uri = URI.parse(url)
request = Net::HTTP::Post.new(uri)
request["WM_SVC.NAME"] = "Walmart Marketplace"
request["WM_QOS.CORRELATION_ID"] = uniqid
request.basic_auth(client_id, client_secret)
request["Accept"] = "application/json"
request.content_type = "application/x-www-form-urlencoded"
request["WM_SVC.VERSION"] = "1.0.0"
req_options = {
use_ssl: uri.scheme == "https",
}
response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
http.request(request)
end
puts "error " + response.code
puts response.body
Now, im getting a 400 error, so something of the data im sending is incorrect or missing... comparing both, i dont have set the postfields option on ruby, for the POST request, not sure if the rest is required as well... any ideas?
Try adding this line:
request.body = 'grant_type=client_credentials'
so I am trying to receive JSON data from one webhook, use PHP to filter for some conditions, and then send the data to an external webhook address based on those conditions.
So for example, I created a php file on my server called "webhook.php":
$dataReceive = file_get_contents("php://input");
$dataEncode = json_encode($dataReceive, true);
print_r($dataEncode);
$curl = curl_init();
$opts = array (
CURLOPT_URL => 'https://hooks.zapier.com/hooks/catch/',
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $dataEncode,
CURLOPT_HTTPHEADER => array (
'Content-type: application/json'
)
);
curl_setopt($curl, $opts);
$results = curl_exec($curl);
echo $results;
curl_close($curl);
The "php://input" can either be exactly as it is, or I tried replacing it with the URL of my webhook.php file just in case. I can test my webhook using Postman, and I am returned a 200 OK, but the data is never sent to my external webhook (https://hooks.zapier.com/hooks/catch/).
I have written the conditional PHP code yet; I just want to ensure I can send and receive this data properly first. Any guidance is much appreciated!
Problem is with curl_setopt. You need to pass three argument for this method curl_setopt ( resource $ch , int $option , mixed $value ). You can set these by following
$curl = curl_init();
$opts = array (
CURLOPT_URL => 'https://hooks.zapier.com/hooks/catch/',
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $dataEncode,
CURLOPT_HTTPHEADER => array (
'Content-type: application/json'
)
);
foreach ($opts as $key => $value) {
curl_setopt($curl, $key, $value);
}
$results = curl_exec($curl);
echo $results;
curl_close($curl);
Or you can set them individually like this
curl_setopt($curl, CURLOPT_URL, 'https://hooks.zapier.com/hooks/catch/');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
.....
I'm using the following code to POST. However, the contents of $_POST are null. What am I missing here?
$orderData = "This is an order";
$json = json_encode($orderData);
curl_setopt_array($curl, array(
CURLOPT_URL => "http://localhost:9999/printpost.php",
CURLOPT_CAINFO => "C:\MAMP\conf\php7.0.0\cacert.pem",
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
"authorization: ".$oauthToken,
"content-type: application/json",
"programid: ".$programId
),
CURLOPT_POSTFIELDS => $json
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
The $_POST superglobal is only used for Content-Type: application/x-www-form-urlencoded; i.e. key/value pairs formatted like foo=1&bar=2. You're posting JSON. Consume it like this on the server side instead of using the superglobal:
$json = file_get_contents('php://input');
Additionally, I recommend the following changes in the cURL client code you posted:
Replace
CURLOPT_CUSTOMREQUEST => "POST",
with
CURLOPT_POST => 1,
And add
CURLOPT_RETURNTRANSFER => 1,
The POST data should not be encoded as JSON, it should be a URL-encoded string in the form name=value&name=value&.... Furthermore, even if JSON were processed, you didn't specify a name for your parameter, so what were you expecting the key in $_POST to be?
If you provide an associative array as CURLOPT_POSTFIELDS, it will automatically encode it for you.
'CURLOPT_POSTFIELDS' => array('orderData' => $orderData)
Then in the server script you can use $_POST['orderData']
I'm new with the Dropbox API integrations, and I'm using the PHP cURL extension to make calls to the HTTP REST API, and when I try to make a request I receive the following string:
Error in call to API function "files/list_folder":
Bad HTTP "Content-Type" header:
"text/plain; boundary=----------------------------645eb1c4046b".
Expecting one of "application/json", "application/json; charset=utf-8",
"text/plain; charset=dropbox-cors-hack".
I'm sending this with code very similar to this:
$sUrl = "https://api.dropboxapi.com/2/files/list_folder";
$oCurl = curl_init($sUrl);
$aPostData = array('path' => '', 'recursive' => true, 'show_hidden' => true);
$sBearer = "MY_TOKEN";
$aRequestOptions = array(
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => array('Content-Type: text/plain',
'Authorization: Bearer ' . $sBearer),
CURLOPT_POSTFIELDS => $aPostData,
CURLOPT_RETURNTRANSFER => true);
curl_setopt_array($aRequestOptions);
$hExec = curl_exec($oCurl);
if ($hExec === false){
// Some error info in JSON format
} else {
var_dump($hExec);
}
As you have it, you're doing a multipart form upload, which isn't what the API expects.
There are a few things you need to do differently:
You should be sending up the parameters as JSON in the body.
You should set the Content-Type to application/json, accordingly.
There isn't a show_hidden parameter on /files/list_folder, but perhaps you meant to send include_deleted.
The curl_setopt_array method takes two parameters, the first of which should be the curl handle.
Here's an updated version of your code that works for me:
<?php
$sUrl = "https://api.dropboxapi.com/2/files/list_folder";
$oCurl = curl_init($sUrl);
$aPostData = array('path' => '', 'recursive' => true, 'include_deleted' => true);
$sBearer = "MY_TOKEN";
$aRequestOptions = array(
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => array('Content-Type: application/json',
'Authorization: Bearer ' . $sBearer),
CURLOPT_POSTFIELDS => json_encode($aPostData),
CURLOPT_RETURNTRANSFER => true);
curl_setopt_array($oCurl, $aRequestOptions);
$hExec = curl_exec($oCurl);
if ($hExec === false){
// Some error info in JSON format
} else {
var_dump($hExec);
}
?>
I have this function here:
public static function call($action, array $args)
{
$post_args = array(
'action' => $action,
'args' => $args
);
$stream = json_encode($post_args);
$headers = array(
'Content-type: application/json',
'Accept: application/json',
'Expect:'
);
$userpwd = self::$_user.':'.sha1(sha1(self::$_pass));
$ch = curl_init();
$args = array(
CURLOPT_URL => self::$_url,
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => $stream,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_USERPWD => $userpwd
);
curl_setopt_array($ch, $args);
$res = curl_exec($ch);
$data = json_decode($res, true);
if (isset($data) === TRUE
&& empty($data) === FALSE
) {
$res = $data;
}
return $res;
}//end call()
and at the URL where I'm posting, I'm just doing:
echo file_get_contents('php://input');
but getting nothing, even though I do post data. What could be the problem? I'm at a dead end.
Also, why do I need CURLOPT_FOLLOWLOCATION set to TRUE when I'm just posting to a simple virtual host URL on my local machine, not doing any redirects.
EDIT:
tried redoing it with fopen like so:
public static function call($action, array $args)
{
$post_args = array(
'action' => $action,
'args' => $args
);
$stream = json_encode($post_args);
$userpwd = self::$_user.':'.sha1(sha1(self::$_pass));
$opts = array(
'http' => array(
'method' => 'POST',
'header' => array(
"Authorization: Basic ".base64_encode($userpwd),
"Content-type: application/json"
),
'content' => $stream
)
);
$context = stream_context_create($opts);
$res = '';
$fp = fopen(self::$_url, 'r', false, $context);
if($fp){
while (!feof($fp)){
$res .= fread($fp, 128);
}
}
return $res;
}//end call()
no success. The connection works with curl and with fopen, since I pass the status along with result (which is just the php://input stream). Any ideas?
Can you be sure about that curl_exec function ends successfully. Also, why don't you use fopen for this purpose. I have written a JSON RPC client and server. I'm sending requests with fopen, and it works perfect.
$httpRequestOptions =
array(
'http'=>array(
'method'=>'POST',
'header'=>'Content-type: application/json',
'content'=>$requestJSON
)
);
$context = stream_context_create($httpRequestOptions);
// send request
if($fileHandler = #fopen($serverURL, 'r', false, $context)){
I'm not writing the rest. You can use this code I have written.
Found out the problem.
I was calling http://localhost/api, since I thought that it would load the index.php automatically, and then I was going to change the default file name for the folder.
The problem was that I didn't add index.php at the end - I should've called http://localhost/api/index.php.
This way it worked with cURL and with fopen.
Any ideas how to call the API without revealing the filename?