It doesn't work and responds with
{"message": "Cannot send an empty message", "code": 50006}
I've tried entering the message into CURLOPT_POSTFIELDS
<?php
$url = 'https://discordapp.com/api/channels/638164827180630046/messages';
$ch = curl_init();
$payload = json_encode( array( "content"=> "a", "nonce"=> "638201106664521728", "tts"=> false ) );
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => array('Authorization: Bot <token>'),
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_VERBOSE => 1,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_POSTFIELDS => "a"
));
$response = curl_exec($ch);
fclose($f);
curl_close($ch);
echo $response;
?>
I want it to send a message
Related
How to send two request with curl using the same headears and params, only the url change
I dont want to duplicate the code two time
$url1 = "www.example1.com"
$url2 = "www.example2.com"
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $config->url1 . '/api/v1/orders',
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POSTFIELDS => json_encode($body),
CURLOPT_HTTPHEADER => $headers,
));
$response = curl_exec($curl);
Thank you
You can put everything into a function and call that:
function get_orders($url)
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url . '/api/v1/orders',
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POSTFIELDS => json_encode($body),
CURLOPT_HTTPHEADER => $headers,
));
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
Call with
$url1 = get_orders('https://example1.com');
$url2 = get_orders('https://example2.com');
Kind of basic:
$urls[] = "https://www.example1.com";
$urls[] = "https://www.example2.com";
foreach ($urls as $url){
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url . '/api/v1/orders',
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POSTFIELDS => json_encode($body),
CURLOPT_HTTPHEADER => $headers,
));
$response = curl_exec($curl);
}
I see a function option was also answered! :) this one is with a loop.
I try to make a cURL request to Dropbox API, but I get this error:
Error in call to API function "files/search_v2": options: expected array, got string
I don't understand why, I'm sending an array!?
My code is below. Documentation here: https://www.dropbox.com/developers/documentation/http/documentation#files-search
$token = "....";
$options = array("path" => "/", "max_results" => 10);
$options["file_categories"] = "folder";
$parameters = array("query" => "searchPhrase", "options" => $options);
$headers = array(
"Authorization: Bearer $token",
'Content-Type: application/json'
);
$curlOptions = array(
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($parameters),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_VERBOSE => true,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false
);
try {
$ch = curl_init('https://api.dropboxapi.com/2/files/search_v2');
curl_setopt_array($ch, $curlOptions);
$response = curl_exec($ch);
//var_dump($response);
curl_close($ch);
} catch (Exception $e) {
trigger_error(
sprintf(
'Curl failed with error #%d: %s',
$e->getCode(),
$e->getMessage()
),
E_USER_ERROR
);
}
Today I was testing PATCH calls to an API using PHP for the first time and to this moment I have not quite figured how to make them work.
I want to add a new parameter with it's value to an already existing set of parameters, I have set CURLOPT_CUSTOMREQUEST => "PATCH" as option and "Content-Type: application/json-patch+json" as the proper header. Despite my body being an array as the error first indicated when I parsed it into JSON, now it says that the entity is still not a well-formed application/json-patch+json.
I am totally lost as to why this could be, since other examples I have seen do not differ from what I wrote.
This is the existing set of parameters:
{
"ac": "774",
"allowed_clis": [
"34774554114"
],
"cc": "34",
"cli": "34774554114",
"id": 1512,
"subscriber_id": 1512
}
What I am trying to achieve:
{
"ac": "774",
"allowed_clis": [
"34774554114"
],
"cc": "34",
"cli": "34774554114",
"id": 1512,
"e164_to_ruri": true,
"subscriber_id": 1512
}
This is my code:
$dataArray = [
'op' => 'add',
'path' => '/e164_to_ruri',
'value' => true
];
function setPreferences($dataArray, $uri, $token){
$ch= curl_init();
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_CUSTOMREQUEST => "PATCH",
CURLOPT_POSTFIELDS => $dataArray,
CURLOPT_URL => $uri,
CURLOPT_HTTPHEADER => array("Content-Type: application/json-patch+json", "Authorization: Basic ".$token)
);
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $response;
}
$response = setPreferences($dataArray, $uri, $token);
$response = json_decode($response, true);
print_r($response);
And finally, the response I get:
Array
(
[message] => The entity is not a well-formed 'application/json-patch+json' document. Malformed JSON: Expected array or object at line 0, offset 0
[code] => 400
)
To my limited knoledge, $dataArray is a well-formed array. So I don't understand where is this response coming from.
Thank you so much for the help provided!
As #UlrichEckhardt pointed out, all I needed to do is turn my data into a JSON array of JSON objects:
function setPreferences($uri, $token){
$ch= curl_init();
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_CUSTOMREQUEST => "PATCH",
CURLOPT_POSTFIELDS => '[{"op": "add", "path": "/e164_to_ruri", "value":true}]',
CURLOPT_URL => $uri,
CURLOPT_HTTPHEADER => array("Content-Type: application/json-patch+json", "Authorization: Basic ".$token)
);
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $response;
}
$response = setPreferences($dataArray, $uri, $token);
$response = json_decode($response, true);
print_r($response);
I have this code:
<?php
$data = array('name' => 'Ross', 'php_master' => true);
$url = 'http://dsaasd.adsds.nl:8081/cops.nsf/orders?openagent';
$html_brand = "www.google.com";
$ch = curl_init();
$options = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 120,
CURLOPT_TIMEOUT => 120,
CURLOPT_MAXREDIRS => 10,
CURLOPT_VERBOSE => true,
);
curl_setopt_array( $ch, $options );
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ( $httpCode != 200 ){
echo "Return code is {$httpCode} \n"
.curl_error($ch);
} else {
echo "<pre>".htmlspecialchars($response)."</pre>";
}
curl_close($ch);
?>
The problem is, if i send curl post to:
$url = 'http://dsaasd.adsds.nl:8081/cops.nsf/orders?openagent';
Then it does NOT work.
But if i send curl post to the same service but on a different server then it works, this is the other server
$url = 'http://dsaasd.adsds.nl:80/cops.nsf/orders?openagent';
I also post data by normal form post to:
$url = 'http://dsaasd.adsds.nl:8081/cops.nsf/orders?openagent';
And then it works and i receive data on the server.
But with this curl post i keep getting:
Return code is 0 Failed to connect to 11.43.45.123: Network is unreachable
Anyone have any idea?
I think you need to add CURLOPT_POST to the $options array. Docs here.
I am trying to convert this VB script to PHP curl
xmlServerHttp.open "POST","url",False xmlServerHttp.setRequestHeader "Content-Type","application/x-www-form-urlencoded"
xmlServerHttp.send "xmlmessage=" & Server.URLEncode(xmlDocument)
‘ xmlDocument = the Xml Document contain the actual request
xmlServerStatus = xmlServerHttp.status
if xmlServerStatus = "200" then
xmlServerResponse = xmlServerHttp.responseText
Else
Response.Appendtolog ".xmlServer status is " & xmlServerStatus
end if
This is what I have so far however it is failing
$curl = curl_init(url);
// Configuring curl options
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-type: application/x-www-form-urlencoded') ,
CURLOPT_POSTFIELDS => $xmldoc,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false
);
// Setting curl options
curl_setopt_array( $curl, $options );
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
// Getting results
echo curl_exec($curl);
The API i am calling return that the xmlmessage variable is not a valid xml document.
try to change $xmlDoc to $xmlDoc->asXML()
like this
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-type: application/x-www-form-urlencoded') ,
CURLOPT_POSTFIELDS => array('xmlmessage='=> $xmlDoc),
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false
);