Twitter API using curl in PHP "direct_messages/new.json" - php

I am using the oauth library to use twitter API for sending the Direct Message to user using curl,but getting the "{"errors":[{"code":215,"message":"Bad Authentication data."}]}".
If i use terminal for curl then it works fine, but getting error while sending through PHP.
<pre>
<?php
error_reporting(1);
require("twitterOauth/autoload.php");
use Abraham\TwitterOAuth\TwitterOAuth;
$text = "Hello, How Are U?";
$headers = array(
'Authorization: OAuth oauth_consumer_key="OAuth oauth_consumer_key",
oauth_nonce="oauth_nonce",
oauth_signature="oauth_signature",
oauth_signature_method="HMAC-SHA1",
oauth_timestamp="1439978004",
oauth_token="oauth_token",
oauth_version="1.0"'
);
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://api.twitter.com/1.1/direct_messages/new.json',
CURLOPT_POST => 1,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_POSTFIELDS => array(
'text' => urlencode($text),
'screen_name' => 'screen_name'
),$headers
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
print_r('Curl error: ' . curl_error($resp));
echo '<pre>'; print_r($resp); die;
// Close request to clear up some resources
curl_close($curl);
?>
</pre>

Related

Using Discord API & cURL to send a Discord DM

How could I send a Discord DM with cURL? I've got it working w/ channel messages but a Discord DM is quite important to my Website to keep users updated. Below is what I've got so far, with the ID being a Discord User ID.
$url = 'https://discordapp.com/api/channels/591765736003731487/messages';
$ch = curl_init();
$f = fopen('request.txt', 'w');
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_STDERR => $f,
));
$response = curl_exec($ch);
fclose($f);
curl_close($ch);
Using your current code, I've made a small snippet. You might need to change a few things according to your needs, but for this matter it works as intended. To make a good use of the CURL request and not make and use repetitive code, I would put it in a function, in this case MakeRequest($endpoint, $data)
Where $endpoint is a String and $data should be an Array
In order to open and send a direct message to a user, you need these endpoints.
For creating a new direct message
POST /users/#me/channels
For sending messages:
POST /channels/{channel.id}/messages
<?php
function MakeRequest($endpoint, $data) {
# Set endpoint
$url = "https://discord.com/api/".$endpoint."";
# Encode data, as Discord requires you to send json data.
$data = json_encode($data);
# Initialize new curl request
$ch = curl_init();
$f = fopen('request.txt', 'w');
# Set headers, data etc..
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => array(
'Authorization: Bot token',
"Content-Type: application/json",
"Accept: application/json"
),
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_VERBOSE => 1,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_POSTFIELDS => $data
CURLOPT_STDERR => $f,
));
$request = curl_exec($ch);
curl_close($ch);
return json_decode($request, true);
}
# Open the DM first
$newDM = MakeRequest('/users/#me/channels', array("recipient_id" => "ID From the user"));
# Check if DM is created, if yes, let's send a message to this channel.
if(isset($newDM["id"])) {
$newMessage = MakeRequest("/channels/".$newDM["id"]."/messages", array("content" => "Hello World."));
}
?>
Heads up: Due security and privacy matters, a direct message might not open if:
The user doesn't share the same server as your bot.
The user has turned off DMs from server members.
The user has blocked your bot.

HTTP Error 400. The request is badly formed in cURL

Here is my code.
<?php
if(isset($_REQUEST['name']) and ($_REQUEST['email']) and ($_REQUEST['msg'])){
$name=$_REQUEST['name'];
$email= $_REQUEST['email'];
$msg = $_REQUEST['msg'];
$url="http://14.140.111.4:20002/ContactUs?name=$name&Email=$email&Message=$msg";
$url = urlencode($url);
$curl = curl_init();
// Set some options - we are passing in a useragent too here urlencode
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_USERAGENT => 'Cubewires Sample cURL Request',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
'name'=>$name,
'Email' => $email,
'Message'=>$msg
)
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
print_r($resp);
// Close request to clear up some resources
curl_close($curl);
}
?>
i got response HTTP Error 400. while using this API data should be inserted in database.
Thanks,
vivek

Unable to make CURL post request using PHP

I read some Q&As but still struggling with this one. I need to post a specific array to an API and get another array as an answer.
UPDATE
I used :
<?php
echo 'Testing cURL<br>';
// Get cURL resource
$curl = curl_init();
$data=array(array("UserId"=>"xxxx-10100","Password"=>"pass"));
$sendpostdata = json_encode( array( "postdata"=> $data ) );
echo $sendpostdata;
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://cloud.servicebridge.com/api/v1/Login',
CURLOPT_HTTPHEADER => array('Accept: application/json','Content-Type: application/json'),
CURLOPT_USERAGENT => 'Codular Sample cURL Request',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $sendpostdata
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
echo $resp;
// Close request to clear up some resources
curl_close($curl);
?>
This results in
Testing cURL
{"postdata":[{"UserId":"xxxxx-10100","Password":"pass"}]}
{ "Data": null, "Success": false, "Error": { "Message": "Invalid UserId: ", "Value": "InvalidUserId", "Code": 9001 } }
No console logs, no other messages or clues
I am trying to implement this API https://cloud.servicebridge.com/developer/index#/
to Worpdress.
The API requires a
{
"UserId": "string",
"Password": "string"
}
Can you help me out? What am I doing wrong
Really appreciate this,
Giannis
I have checked the given API and it's REQUEST Parameters to access them and I think your initial data (username and password) is not going correctly. Please use this code:-
<?php
error_reporting(E_ALL); // check all type of errors
ini_set('display_errors',1); // display those errors
// Get cURL resource
$curl = curl_init();
$sendpostdata = json_encode(array("UserId"=>"xxxx-10100","Password"=>"pass"));
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://cloud.servicebridge.com/api/v1/Login',
CURLOPT_HTTPHEADER => array('Accept: application/json','Content-Type: application/json'),
CURLOPT_USERAGENT => 'Codular Sample cURL Request',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $sendpostdata
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
echo $resp;
// Close request to clear up some resources
curl_close($curl);
?>
Note:- change UserId & Passwordvalues to your real values.Thanks
This might help you in generating proper json. Hope this will work.
use this
$data=array(array("UserId"=>"xxxxx-10100","Password"=>"pass"));
instead of
$data ='[{"UserId":"xxxxx-10100","Password": "pass"}]';

Error to call Tinypass response in Restapi

I am new in Tinypass api integration.
I try to integrate Tinypass API using PHP. Code below:
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'http://sandbox.tinypass.com/r2/access?rid=portfolio_id&user_ref=badashah26',
// CURLOPT_USERAGENT => 'Codular Sample cURL Request',
CURLOPT_HTTPHEADER => array(
'AID: xxxxxxxx', // PUT your AID
'signature: xxxxxxxxxxxxxxxxxx', // PUT your signature
'sandbox: true'
)
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
print_r($resp); exit();
Response get but error display.
"error":{"message":"Access denied: invalid AID or signature","code":401}}
Any one can find solutions.
Thanks
Our REST API documentation has been updated to provide a few steps on how to generate your own API header. Check out the documentation at http://developer.tinypass.com/main/restapi.
Best,
Tinypass Support
Something like this..? (untested)
$aid = 'YOUR AID';
$action = '/r2/access?rid='.$rid.'&user_ref='.$userref;
$request = 'GET '.$action;
$url = 'http://sandbox.tinypass.com'.$action;
$signature = hash_hmac('sha256',$request,$aid);
$auth = $aid.':'.$signature;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => array('Authorization: '.$auth);
));
$resp = curl_exec($curl);
curl_close($curl);
print_r($resp); exit();

custom http request header in php

I have a rest api made using philsturgeon/codeigniter-restserver.I have set the authentication
api key for the rest api.So that all request will be authenticated usingthe api key.
The request parameter for apikey is X-API-KEY and the key should be sent as request parameter in the header.
But I cant get itworking using curl.But in rest console and post man,I can properly set the request parameter in the header.But i cant do it with curl.
<?php
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => 'www.exampleapi.come',
CURLOPT_USERPWD => 'martinbean:4eefab4111b2a',
CURLOPT_HTTPHEADER => 'X-API-KEY:123456789'
));
$response = curl_exec($ch) ;
echo $response ;
?>
I have tried this,but itsnot working,its showing invalid api key
How to do this part using curl?
The php curl wrapper documentation says that you need to pass an array to this option.
See : http://php.net/manual/en/function.curl-setopt.php
So this may work :
<?php
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => 'www.exampleapi.come',
CURLOPT_USERPWD => 'martinbean:4eefab4111b2a',
CURLOPT_HTTPHEADER => array('X-API-KEY:123456789')
));
$response = curl_exec($ch) ;
echo $response ;
?>

Categories