Sendgrid adding to mail list not working - php

I am using the following code for adding emails to a list in sendgrid. But it is returning inserted :0 response
$request_url = "https://sendgrid.com/api/newsletter/lists/email/add.json";
$data = array("email"=>"testemail#test.com");
$params = array(
'api_user' => $sengrid_user,
'api_key' => $sendgrid_pass,
'list'=>"TestAlwin",
'data' =>json_encode($data)
);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $request_url);
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$resp = curl_exec($ch);
curl_close($ch);
This is returning inserted :0 response. It should insert the mailid to the list I have specified.
I am using the following as reference :
curl -d 'api_user=your_sendgrid_username&api_key=your_sendgrid_password&list=my_list&data[]={"email":"address1#domain.com","name":"contactName1"}&data[]={"email":"address2#domain.com","name":"contactName2"}' https://sendgrid.com/api/newsletter/lists/email/add.json
This is actually given in their api here :
http://sendgrid.com/docs/API_Reference/Marketing_Emails_API/emails.html
and I am adding the curl vebrose here :
* About to connect() to sendgrid.com port 80 (#0)
* Trying 1.1.1.1... * connected
* Connected to sendgrid.com (1.1.1.1) port 80 (#0)
> POST /api/newsletter/lists/email/add.json?list=TestAlwin HTTP/1.1
Host: sendgrid.com
Accept: */*
Content-Length: 395
Expect: 100-continue
Content-Type: multipart/form-data; boundary=----------------------------4435bfc2eb00
< HTTP/1.1 100 Continue
< HTTP/1.1 200 OK
< Server: nginx
< Date: Fri, 13 Sep 2013 07:04:42 GMT
< Content-Type: text/html
< Transfer-Encoding: chunked
< Connection: keep-alive
< Vary: Accept-Encoding
<
* Connection #0 to host sendgrid.com left intact
* Closing connection #0
These 1.1.1.1 is just a test IP that I have added here instead of the actual one.

The SendGrid Newsletter API is broken- specifically you can't add more than a single email to a list at a time, making it unusable. I reported it a week ago- they confirmed the bug, gave no timeline for a fix. They don't seem too concerned...

The SendGrid Newsletter API requires both 'email' and 'name' parameters at a minimum (as seen in the docs: http://sendgrid.com/docs/API_Reference/Marketing_Emails_API/emails.html)
I updated your test code to include "name" => '' to the code and it works beautifully (also fixed typo in the api_user's $sendgrid_user variable).
Cheers!
--Jayson
$request_url = "https://sendgrid.com/api/newsletter/lists/email/add.json";
$data = array("email" => "hello#world.com", "name" => '');
$params = array(
'api_user' => $sendgrid_user,
'api_key' => $sendgrid_pass,
'list'=>"TestAlwin",
'data' =>json_encode($data)
);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $request_url);
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$resp = curl_exec($ch);
curl_close($ch);

Related

"WISO mein Büro" API doesnt work or it is my fault

unfortunately I get no good response from the api provider.
No I need your support. I build a test extension in "wiso mein büro" for testing.
I would like to create an order with php and this api:
https://api.meinbuero.de/openapi/documentation/#/orders/postorderCreate
My code:
$wisoApiKey = "XXX";
$wisoSecretApiKey = "XXX";
$wisoOwnershipId = "XXX";
$positionen[] = array ('id' => 769541, 'amount' => 60);
$ch = curl_init('https://api.meinbuero.de/openapi/order/');
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json" , "Authorization: Bearer ".$wisoToken));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_USERPWD, $wisoApiKey . ":" . $wisoSecretApiKey);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(
array (
"customerId" => 1698487,
"articles" => $positionen
)
));
$createOrder = curl_exec($ch);
curl_close($ch);
echo '<pre>';
print_r($createOrder);
Result:
HTTP/1.1 500 Internal Server Error
Server: nginx/1.19.0
Date: Mon, 09 Aug 2021 18:38:49 GMT
Transfer-Encoding: chunked
Connection: keep-alive
Strict-Transport-Security: max-age=7776000; includeSubDomains; preload
Access-Control-Allow-Origin: *
All other apis works fine like create, get customer, create invoice from order, ...
Only the api create order doesn't work for me.
I don't know why ...

PayPal NVP API - TransactionSearch Error 10001 Timeout processing request

I'm trying to build a custom accounting report using the PayPal NVP API that will get all transactions for a specific date range.
My code:
$headers = array(
'USER' => $production_user,
'PWD' => $production_pass,
'SIGNATURE' => $production_sig
);
$nvp = array(
'METHOD' => 'TransactionSearch',
'TRANSACTIONCLASS' => 'RECEIVED',
'STARTDATE' => '2016-12-01T00:00:00Z',
'ENDDATE' => '2016-12-31T00:00:00Z'
);
$request_url = "https://api-3t.paypal.com/nvp?".http_build_query($nvp);
$curl = curl_init($request_url);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HEADER, $headers);
curl_setopt($curl, CURLOPT_POST, 1);
$result = curl_exec($curl);
$result = explode("&", $result);
foreach($result as $f=>$v){
$t = explode("=", $v);
echo $t[0]." => ".urldecode($t[1]);
echo "<br>";
}
Here is what gets printed:
HTTP/1.1 200 OK Date: Fri, 10 Feb 2017 19:51:20 GMT Server: Apache X-PAYPAL-OPERATION-NAME: X-PAYPAL-API-RC: 10001 Connection: close Cache-Control: max-age => 0, no-cache, no-store, must-revalidate Pragma: no-cache HTTP_X_PP_AZ_LOCATOR: slcb.slc Paypal-Debug-Id: 484a759b46e4a Set-Cookie: X-PP-SILOVER
CORRELATIONID => some_random_characters
ACK => Failure
L_ERRORCODE0 => 10001
L_SHORTMESSAGE0 => Internal Error
L_LONGMESSAGE0 => Timeout processing request
Any assistance with this issue would be greatly appreciated!
You are using a post request and have not post data.
I do not know what PayPal wants to see but I'm guessing it's not what you are sending.
Some things to try:
If you need to pass the USER, PWD, and SIGNATURE in the Request Header do it like this:
$request = array();
$request[] = "USER: $production_user";
$request[] = "PWD: $production_pass";
$request[] = "SIGNATURE: $production_sig";
curl_setopt($ch, CURLOPT_HTTPHEADER, $request);
If the $nvp parameters need to be post data, try this:
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $nvp);
The $nvp can be passed as a query string in the post data also.
$query = http_build_query($nvp);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
The difference is the Content-Type.
The first method:
Content-Type: application/x-www-form-urlencoded
The second method:
Content-Type: multipart/form-data
To help in trouble shooting it would be good to see both the request and response header.
Use these options to get the headers:
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
The Request Header will be in the 'curl_getinfo()'
curl_setopt($ch, CURLOPT_HEADER, true);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
var_export($info);
The above gives lots of other details of the request. If you only want to see the header:
$request = curl_getinfo($ch, CURLINFO_HEADER_OUT);
To get the response header:
$result = curl_exec($ch);
$skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE));
$response = substr($result ,0,$skip);
$result = substr($result ,$skip);

php - CURL HTTP Authentication at server side

i want to change xml value in php but don't change ....
<?php
$url = "http://192.168.1.103:8080/ew.xml";
$xml = '<?xml version="1.0" ?>
<DDCConfig:getValue xmlns:DDCConfig="urn:SMUDDCConfiguration">
<DDCConfig:Network>
<DDCConfig:LocalIP>192.168.103.223</DDCConfig:LocalIP>
<DDCConfig:GlobalIP>168.188.127.123</DDCConfig:GlobalIP>
<DDCConfig:RootBridge>Yes</DDCConfig:RootBridge>
</DDCConfig:Network>
</DDCConfig:getValue>';
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_USERPWD, "lmk:alrud89");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_URL, "$url");
curl_setopt($ch, CURLOPT_PORT, 8080);
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,array($xml));
$http_result = curl_exec($ch);
if($http_result){
echo $http_result;
}else{
echo curl_error($ch);
}
curl_close($ch);
?>
error message is
* About to connect() to 192.168.1.103 port 8080 (#0)
* Trying 192.168.1.103...
* connected
* Server auth using Basic with user 'lmk'
> PUT /ew.xml HTTP/1.1
> Authorization: Basic bG1rOmFscnVkODk=
> Host: 192.168.1.103:8080 Accept: /
> Transfer-Encoding: chunked
> Expect: 100-continue
< HTTP/1.1 401 Unauthorized
< Content-Length: 0
<
> WWW-Authenticate: Digest qop="auth", realm="mydomain.com",
nonce="1366254379"
* HTTP error before end of send, stop sending
* Closing connection #0
what is the problem & solution?
Your credentials are incorrect! (that's what error 401 means)
There's a wonderful plugin for Firefox called poster - install it and use it to double-check that the request is formatted correctly.
By the way, you might want to add the following header:
curl_setopt ($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/xml"))
Another thing: PUT is not always supported, try POST - sometimes it'll work.

Making a PUT request using CURL in PHP

I am using PHP 5.3.6 and it seems I am unable to make a PUT request using CURL for PUTting just a string.
function put_data($url, $data)
{
$useragent="SimpleAgent-1.0";
$fh = fopen('php://memory', 'rw');
fwrite($fh, $data);
rewind($fh);$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_INFILE, $fh);
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($data));
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
fclose($fh);
return $result;
}
Here, $data is the string that I want to PUT.
Doesn't work and returns the following error:
500 Internal Server Error The server has either erred or is incapable
of performing the requested operation.
expected string or buffer
I used your code so defined a url and filled the data with a string and all worked as expected. Being I was rejected by the website as there was no receiving end that could deal with a put. To get info easily just add the line
curl_setopt($ch, CURLOPT_VERBOSE, true);
and you will get something like:
* About to connect() to yyyy.xxxx.com port 80 (#0)
* Trying 62.221.196.28...
* connected
* Connected to yyyy.xxxx.com (zz.221.196.28) port 80 (#0)
> PUT / HTTP/1.1
User-Agent: SimpleAgent-1.0
Host: yyyy.xxxx.com
Accept: */*
Content-Length: 14
Expect: 100-continue
< HTTP/1.1 100 Continue
* We are completely uploaded and fine
< HTTP/1.1 405 Method Not Allowed
< Date: Thu, 09 Feb 2012 19:46:28 GMT
< Server: Apache
< Allow: GET,HEAD,POST,OPTIONS
< Vary: Accept-Encoding
< Content-Length: 231
< Content-Type: text/html; charset=iso-8859-1
<
* Connection #0 to host yyy.xxxx.com left intact
* Closing connection #0
As you can see from the logging the request went out, however when you want to put the data, the apache setting doesn't allow you to put data to that url. So depending upon the server you will have to take care of a receiving url that accepts the PUT.
I am only able to pass the array as a data with the versions I am using. So this is what I am doing now:
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
curl_setopt($ch, CURLOPT_COOKIE, $curl_cookie);
$arr = array();
if (isset($data)) {
$arr['my_data'] = $data;
}
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($arr));
curl_exec($ch);

C2DM 411. That’s an error. POST requests require a Content-length header

Am working on a push notification that makes user of C2DM. I have been able to obtain the users registration id, authentication token from Google. But when i try to send the message i receive this error from google "411. That’s an error. POST requests require a Content-length header".
function send($deviceRegistrationId, $msgType, $messageText) {
$f = fopen('request.txt', 'w');
$reg_id = $deviceRegistrationId; // Registration ID
$device_id = "1";
$data = array(
'registration_id' => trim($reg_id),
'collapse_key' => 'ck_'.trim($device_id),
'data.arg' => trim($messageText)
);
$dataStr = http_build_query($data);
$headers = array(
'Authorization: GoogleLogin auth='.$_SESSION['google'],
'Content-Type: application/x-www-form-urlencoded',
'Content-Length: '.strlen($dataStr)
);
// Prepare the cURL request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://android.apis.google.com/c2dm/send");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataStr);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_STDERR, $f);
// Send the request and echo the response body
$response = curl_exec($ch);
fclose($f);
echo "Reponse is ".$response;
}
Also below is the result i get when i output values of curl to a file
About to connect() to android.apis.google.com port 443 (#0)
Trying 74.125.39.139... * connected
Connected to android.apis.google.com (74.125.39.139) port 443 (#0)
successfully set certificate verify locations:
CAfile: none
CApath: /etc/ssl/certs
SSL connection using ECDHE-RSA-RC4-SHA
Server certificate:
subject: C=US; ST=California; L=Mountain View; O=Google Inc; CN=*.google.com
start date: 2011-11-10 07:48:51 GMT
expire date: 2012-11-10 07:58:51 GMT
subjectAltName: android.apis.google.com matched
issuer: C=US; O=Google Inc; CN=Google Internet Authority
SSL certificate verify ok.
POST /c2dm/send HTTP/1.1
Host: android.apis.google.com
Accept: /
Authorization: GoogleLogin auth=DQAAAMIAAADlJmeJmrTjmzdAbt1HiMvvVj_vdSduVXrkEFk_D19OG0o->FIn1ZzJ25d3MZfDTK2QErF_jEFAndPgC3RoGif6V-gs9w3-FA7VaEWd62qNPnscsqi1j6R0b0J5vtOwGItNmuXm5n1MZrOZ4sd3yx_D95rtzriymmyhilzLWNAyNjPO6FsmX-4Ty_3OwPaw02qe_oHeSvTNt7s6SW-_kT-T1hdJuywCoSf5p2esSzk9sUj9YDwtEXPneDIaB1z2Qy6NcMBjYY8X185GctBttXrjd
Content-Type: application/x-www-form-urlencoded
Content-Length: 207
HTTP 1.0, assume close after body
HTTP/1.0 411 Length Required
Content-Type: text/html; charset=UTF-8
Content-Length: 11791
Date: Sun, 27 Nov 2011 12:16:06 GMT
Server: GFE/2.0
Closing connection #0
how do i resolve this?
function send ($deviceRegistrationId, $msgType, $messageText) {
$reg_id = $deviceRegistrationId; // Registration ID
$device_id = "1";
// Build request body
$data = array (
'registration_id' => trim($reg_id),
'collapse_key' => 'ck_'.trim($device_id),
'data.arg' => trim($messageText)
);
$dataStr = trim(http_build_query($data));
// Headers for the request
$headers = array(
'Authorization: GoogleLogin auth='.trim($_SESSION['google']),
'Content-Type: application/x-www-form-urlencoded',
'Content-Length: '.trim(strlen($dataStr)),
'Connection: close'
);
// Prepare the cURL request
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => "https://android.apis.google.com/c2dm/send",
CURLOPT_HTTPHEADER => $headers,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $dataStr,
CURLOPT_RETURNTRANSFER => true
));
// For debugging
//$f = fopen('request.txt', 'w');
//curl_setopt($ch, CURLOPT_VERBOSE, true);
//curl_setopt($ch, CURLOPT_HEADER, true);
//curl_setopt($ch, CURLOPT_STDERR, $f);
// Send the request and echo the response body
$response = curl_exec($ch);
//fclose($f);
echo "Reponse is ".$response;
}
Some thing related to this is here http://codershelpingcoders.com.Have a look

Categories