I'm using Sendgrid API to send an email message over HTTP via PHP. This is my code:
<?php
$url = 'https://api.sendgrid.com/';
$user = 'USER';
$pass = 'PASSWORD';
$params = array(
'api_user' => $user,
'api_key' => $pass,
'to' => 'TARGET',
'subject' => 'Kami Menanti Anda',
'from' => 'noreply#kompetisiindonesia.com',
);
$params['html'] = 'html message';
$params['text'] = $params['html'];
$request = $url.'api/mail.send.json';
// Generate curl request
$session = curl_init($request);
// Tell curl to use HTTP POST
curl_setopt ($session, CURLOPT_POST, true);
// Tell curl that this is the body of the POST
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
// Tell curl not to return headers, but do return the response
curl_setopt($session, CURLOPT_HEADER, false);
// Tell PHP not to use SSLv3 (instead opting for TLS)
curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// obtain response
$response = curl_exec($session);
curl_close($session);
// print everything out
print_r($response);
But I get this error message:
Notice: Use of undefined constant CURL_SSLVERSION_TLSv1_2 - assumed
'CURL_SSLVERSION_TLSv1_2' in
/opt/lampp/htdocs/oprek/sendgrid/sendviahttp.php on line 28
Does anybody know what's happening?
It looks like you might have an outdated build of CURL installed on your machine.
CURL_SSLVERSION_TLSv1_2 (integer) Available since PHP 5.5.19 and 5.6.3
http://php.net/manual/en/curl.constants.php
Depending on your situation, you can try using the integer that represents the constant. In our case (CentOS6, with IUS PHP 5.3.23), the constant was not there, but the following worked just fine..
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://tlstest.paypal.com/");
curl_setopt($ch, CURLOPT_SSLVERSION, 6);
var_dump(curl_exec($ch));
if ( ! defined('CURL_SSLVERSION_TLSv1_2')) {
define('CURL_SSLVERSION_TLSv1_2', 6);
}
Add above code before
curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
Also, check the namespace your code is running in. If you have a TYPO3-command-controller, it`s code will run in \TYPO3 - namespace. Constants defined in the regular normal namespace have to be prepended with a backslash then, to be evaluated correctly.
Related
I want send post request from php to python and get answer
I write this script which the send post
$url = 'http://localhost:8080/cgi-bin/file.py';
$body = 'hello world';
$options = array('method'=>'POST',
'content'=>$body,
'header'=>'Content-type:application/x-ww-form-urlencoded');
$context = stream_context_create(array('http' => $options));
print file_get_contents($url, false,$context);
I'm use custom python server
from http.server import HTTPServer, CGIHTTPRequestHandler
server_address = ("", 8080)
httpd = HTTPServer(server_address, CGIHTTPRequestHandler)
httpd.serve_forever()
And python script which the takes post request
print('Content-type: text/html\n')
import cgi
form = cgi.FieldStorage()
text2 = form.getfirst("content", "empty")
print("<p>TEXT_2: {}</p>".format(text2))
And then I get
write() argument must be str, not bytes\r\n'
How can it be solved?
P.S Sorry for my bad english
Check curl extension for php http://php.net/manual/en/book.curl.php
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://localhost:8080/cgi-bin/file.py");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"postvar1=value1&postvar2=value2&postvar3=value3");
// in real life you should use something like:
// curl_setopt($ch, CURLOPT_POSTFIELDS,
// http_build_query(array('postvar1' => 'value1')));
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
You can also use a library like guzzle that may have some other bells and whistles you may want to use.
Example usage can be found on this other answer here:
https://stackoverflow.com/a/29601842/6626810
I am building my first ever project from scratch on a lamp stack. I decided to try out the slim api framework. Below you can see i start building a helper function for my api. However I am getting this
error: undefined constant CURLOPT_GET - assumed 'CURLOPT_GET'
and then this
error: curl_setopt() expects parameter 2 to be long, string given
// Main Gospel Blocks API Call Function
Function gbCall($gbRoute) {
// JSON Headers
$gblCallHeaders[] = "Content-Type: application/json;charset=utf-8";
// Call the API
$gblCall = curl_init();
curl_setopt($gblCall, CURLOPT_URL, $GLOBALS['gbApiUrl'] . $gbRoute);
curl_setopt($gblCall, CURLOPT_GET, TRUE);
curl_setopt($gblCall, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($gblCall, CURLOPT_HTTPHEADER, $gblCallHeaders);
// Get the response
$response = curl_exec($gblCall);
// Close cURL connection
curl_close($gblCall);
// Decode the response (Transform it to an Array)
$response = json_decode($response, true);
// Return response
return $response;
}
The api I am hitting is just json encoded objects, not quite sure why this isn't returning the json...
Try using CURLOPT_HTTPGET though I am not sure if it serves your purpose.
More detail can be found here
It happens when phpxxx-curl was not installed in your machine
There is nothing like CURLOPT_GET in the options for cURL that's why that error occured. Take a look at CURL options
For the GET Request in the Curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "URL");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$headers = array();
$headers[] = "Key: Value";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
hello all please take a look at the below codes and help me to figure out the issue ..
$tos='soanm2#gmail.com';
$subject09='hello madam223';
$messageto90='this is a test';
$myName_emailis='Sonam verma';
$emaillist_act=array('sonam#gmail.com','sakshi#gmail.com');
$json_string = array( 'to' =>$emaillist_act,'category' => 'activity');
$url = 'https://api.sendgrid.com/';
$user = 'username';
$pass = 'password';
$params = array(
'api_user' => $user,
'api_key' => $pass,
'x-smtpapi' => json_encode($json_string),
'to' => "$tos",
'subject' => "$subject09",
'html' => "$messageto90",
'fromname' => $myName_emailis,
'from' => "domain.com <contact#doamian.com>"
);
$request = $url.'api/mail.send.json';
// Generate curl request
$session = curl_init($request);
// Tell curl to use HTTP POST
curl_setopt ($session, CURLOPT_POST, true);
// Tell curl that this is the body of the POST
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
// Tell curl not to return headers, but do return the response
curl_setopt($session, CURLOPT_HEADER, false);
// Tell PHP not to use SSLv3 (instead opting for TLS)
#curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// obtain response
$response = curl_exec($session);
curl_close($session);
print_r($response);
?>
This code works on other server http://www.reurl.in/6626fdd53
but it dosent do anything on my server http://www.reurl.in/26cbacc87
it dosent prints aything it is supposed to print the response success or fail etc..
This code works great on both the server.
<?php
//Your authentication key
$authKey = "somethingapikey";
//Multiple mobiles numbers separated by comma
$mobileNumber = "$mobileno";
//Sender ID,While using route4 sender id should be 6 characters long.
$senderId = "something";
//Your message to send, Add URL encoding here.
if(strlen($textsms) > 300){
$message = urlencode(substr($textsms,0,300));
$message=$message.'...'. ' www.domain.com';
}else{
$textsms=$textsms.' www.domain.com';
$message = urlencode($textsms);
}
//Define route
$route = "domain";
//Prepare you post parameters
$postData = array(
'authkey' => $authKey,
'mobiles' => $mobileNumber,
'message' => $message,
'sender' => $senderId,
'route' => $route
);
//API URL
$url="https://control.msg91.com/api/sendhttp.php";
// init the resource
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postData
//,CURLOPT_FOLLOWLOCATION => true
));
//Ignore SSL certificate verification
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
//get response
$output = curl_exec($ch);
//Print error if any
//if(curl_errno($ch))
//{
// echo 'error:' . curl_error($ch);
//}
curl_close($ch);
//echo $output;
?>
Please help me narrow down the issue as this is very important for me please ...
My server admin says they cant help .
i also narrowed the issue we have curl version 7.38.0 and other server has 7.19.7
All the error reportings are on but still i am not getting anything no errors no success no mail.
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
After adding the
$error = curl_error($session);
echo $error;
i get
Unsupported SSL protocol version
For quick fix, Add curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);
$tos='soanm2#gmail.com';
$subject09='hello madam223';
$messageto90='this is a test';
$myName_emailis='Sonam verma';
$emaillist_act=array('sonam#gmail.com','sakshi#gmail.com');
$json_string = array( 'to' =>$emaillist_act,'category' => 'activity');
$url = 'https://api.sendgrid.com/';
$user = 'username';
$pass = 'password';
$params = array(
'api_user' => $user,
'api_key' => $pass,
'x-smtpapi' => json_encode($json_string),
'to' => "$tos",
'subject' => "$subject09",
'html' => "$messageto90",
'fromname' => $myName_emailis,
'from' => "domain.com <contact#doamian.com>"
);
$request = $url.'api/mail.send.json';
// Generate curl request
$session = curl_init($request);
// Tell curl to use HTTP POST
curl_setopt ($session, CURLOPT_POST, true);
// Tell curl that this is the body of the POST
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
// Tell curl not to return headers, but do return the response
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);
// Tell PHP not to use SSLv3 (instead opting for TLS)
#curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// obtain response
$response = curl_exec($session);
curl_close($session);
print_r($response);
?>
Warning: Setting CURLOPT_SSL_VERIFYPEER to false allows for man-in-the-middle-attacks.
Another Solution
If your PHP installation doesn't have an up-to-date CA root certificate bundle, download the one at the curl website and save it on your server:
http://curl.haxx.se/docs/caextract.html
Then set a path to it in your php.ini file, e.g. on Windows:
curl.cainfo=c:\php\cacert.pem
Turning off CURLOPT_SSL_VERIFYPEER allows man in the middle (MITM) attacks, which you don't want!
Try to use a different SSL protocol version.
Change the line:
#curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
Try these possible constants to see which one works:
CURL_SSLVERSION_TLSv1
CURL_SSLVERSION_SSLv2
CURL_SSLVERSION_SSLv3
CURL_SSLVERSION_TLSv1_0
CURL_SSLVERSION_TLSv1_1
CURL_SSLVERSION_TLSv1_2
Also: Remove the # from the beginning of the line. The #suppresses errors. You do want to get errors when any occur.
If neither works, maybe support for the required ssl protocol version is not installed on the server. You would then need to update the CURL used or the OPENSSL used on the server.
I'm pretty new using third party API's and I need to obtain some data that is formatted in JSON. Documentation here.
$url = 'https://api.tapjoy.com/reporting_data.json';
$vars = '?email=myname%40email.com
&api_key=123456789
&date=YYYY-MM-DD
[&page=NUM]
[&timezone=NUM]';
$parameters = array('email' => 'myemail#me.com'
'api_key', => '123456789',
'' => '2014-12-22');
$response = http_get($url, $parameters, $info);
print_r($info);
I'm not sure if something may be wrong with the way localhost is configured that may be causing this error, but this is what it says in the MAMP error log.
PHP Fatal error: Call to undefined function http_get()
So, I tried this approach next.
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
curl_close($ch);
var_dump(#json_decode($data, true));
This produces
NULL
From my experiences, using file_get_contents has always sufficed for me. So, trying cURL is a bit foreign to me.
Any ideas?
For PHP Fatal error: Call to undefined function http_get(),
I believe the php_http.dll extension is not enabled.
;extension=php_http.dll
Remove the ; colon from above line and should be like as below
extension=php_http.dll
Finally restart the apache and make sure it enabled using phpinfo(); function
I'm trying to create a script that will delete all user properties for a particular individual. I'm able to use an api call to get the users' properties. And I'm trying use a delete api to remove each property. But I'm having an issue doing so. Below is the code:
$delete = "http://www.ourwiki.com/#api/DELETE:users/$user_id/properties/%s";
$xml = new SimpleXMLElement($xmlString);
foreach($xml->property as $property) {
$name = $property['name']; // the name is stored in the attribute
file_get_contents(sprintf($delete, $name));
}
I believe I need to use curl to perform the actual delete. Here is an example of that command (property=something):
curl -u username:password -X DELETE -i http://ourwiki.com/#api/users/=john_smith#ourwiki.com/properties/something
-u Provides external user authentication.
-X Specifies the HTTP request method.
-i Outputs the HTTP response headers. Useful for debugging.
Is this something that I can incorporate right into the existing script? Or is there something else I need to do? Any help would be greatly appreciated.
update:
<?php
$user_id="john_smith#ourwiki.com";
$url=('http://aaron:12345#192.168.245.133/#api/deki/users/=john_smith#ourwiki.com/properties');
$xmlString=file_get_contents($url);
$delete = "http://aaron:12345#192.168.245.133/#api/deki/DELETE:users/$user_id/properties/%s";
$xml = new SimpleXMLElement($xmlString);
function curl_fetch($url,$username,$password,$method='DELETE')
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // returns output as a string instead of echoing it
curl_setopt($ch,CURLOPT_USERPWD,"$username:$password"); // if your server requires basic auth do this
return curl_exec($ch);
}
foreach($xml->property as $property) {
$name = $property['name']; // the name is stored in the attribute
curl_fetch(sprintf($delete, $name),'aaron','12345');
}
?>
You can use php curl, or shell out to curl using exec.
If curl is already enabled on you web server, go with php curl. If you cant install php-curl copy a command line version of curl and you are good to go.
In php-curl to set the delete method do:
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
edit
Something like this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.ourwiki.com/#api/whatever/url/you/want/or/need");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // returns output as a string instead of echoing it
curl_setopt($ch,CURLOPT_USERPWD,"$username:$password"); // if your server requires basic auth do this
$output = curl_exec($ch);
edit2
stick that above code in a function:
function curl_fetch($url,$username,$password,$method='DELETE')
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // returns output as a string instead of echoing it
curl_setopt($ch,CURLOPT_USERPWD,"$username:$password"); // if your server requires basic auth do this
return curl_exec($ch);
}
and replace the call to file_get_contents() in your script with the new function.
curl_fetch(sprintf($delete, $name),'aaron','12345');
Done.
looks like you are looking for the curl function calls in php, including curl_setopt
See: http://php.net/curl