So I've been trying to figure out how to manage this for a few days but nothing so far...
Here's my problem : I'm trying to retrieve a json object from a SendGrid GET API.
-> https://api.sendgrid.com/v3/suppression/bounces/
Which needs cURL authentication, as the support told me :
curl --request GET \
--url https://api.sendgrid.com/v3/suppression/bounces/
--header 'authorization: Bearer YOUR_API_KEY'
--header 'Content-Type: application/json'
So since I'm pretty new to cURL, I can't manage to retrieve the datas with this :
$ch = curl_init('https://api.sendgrid.com/v3/suppression/bounces/');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , 'authorization: Bearer MY_API_KEY'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$bounces = json_decode(file_get_contents($response),true);
I get this error :
Warning: file_get_contents({"errors":[{"field":null,"message":"resource not found"}]}): failed to open stream: No such file or directory in ...
Thanks ! :)
Try to use the curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.sendgrid.com/v3/suppression/bounces/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$headers = array();
$headers[] = "Authorization: Bearer YOUR_API_KEY";
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
Edited
based on your JSON response I could try to parse with these
$response = '[{"created":1495048030,"email":"dfezfezfezfezfezfez#exelcia-it.com","reason":"550 5.4.1 [dfezfezfezfezfezfez#exelcia-it.com]: Recipient address rejected: Access denied [DB5EUR01FT015.eop-EUR01.prod.protection.outlook.com] ","status":"5.4.1"},{"created":1494945503,"email":"squestgel#exelcia-it.com","reason":"550 5.4.1 [squestgel#exelcia-it.com]: Recipient address rejected: Access denied [DB5EUR01FT004.eop-EUR01.prod.protection.outlook.com] ","status":"5.4.1"}]';
$json = json_decode($response , true) ;
echo '<pre>'.print_r($json, true).'</pre>';
foreach ($json as $data) {
echo $data['email'].'<br>';
}
And here is the result I got.
Array
(
[0] => Array
(
[created] => 1495048030
[email] => dfezfezfezfezfezfez#exelcia-it.com
[reason] => 550 5.4.1 [dfezfezfezfezfezfez#exelcia-it.com]: Recipient address rejected: Access denied [DB5EUR01FT015.eop-EUR01.prod.protection.outlook.com]
[status] => 5.4.1
)
[1] => Array
(
[created] => 1494945503
[email] => squestgel#exelcia-it.com
[reason] => 550 5.4.1 [squestgel#exelcia-it.com]: Recipient address rejected: Access denied [DB5EUR01FT004.eop-EUR01.prod.protection.outlook.com]
[status] => 5.4.1
)
)
dfezfezfezfezfezfez#exelcia-it.com
squestgel#exelcia-it.com
I managed to do what I wanted to do :
//opening curl sessions
$ch1 = curl_init('https://api.sendgrid.com/v3/suppression/bounces');
$ch2 = curl_init('https://api.sendgrid.com/v3/suppression/invalid_emails');
//setting options to ch1
curl_setopt($ch1, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , 'authorization: Bearer MY_API_KEY'));
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
//setting options to ch2
curl_setopt($ch2, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , 'authorization: Bearer MY_API_KEY'));
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
//exec 2 curl sessions
$resp1 = curl_exec($ch1);
$resp2 = curl_exec($ch2);
//Merging 2 curl arrays and converting into json
$array = json_encode(array_merge(json_decode($resp1, true),json_decode($resp2, true)));
//Decoding result
$arr = json_decode($array, true);
foreach ($arr as $data) {
echo $data['email'].'<br>';
}
Related
I like to use the command function of the SDM API. I have to do it in PHP
The Google Documentation requires the following Syntax :
curl -X POST \
'https://smartdevicemanagement.googleapis.com/v1/enterprises/project-id/devices/device-id:executeCommand' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer access-token' \
--data-raw '{
"command" : "sdm.devices.commands.CameraLiveStream.GenerateRtspStream",
"params" : {}
}'
My PHP Code looks like this :
$sub ['eventId'] = $event_id;
$PostData_array = array('command' => "sdm.devices.commands.CameraLiveStream.GenerateRtspStream" ,
'params' => $sub
);
$PostData = json_encode ($PostData_array);
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('Authorization: Bearer '.$access_token ));
curl_setopt($curl_handle, CURLOPT_POST, true);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $PostData);
curl_setopt($curl_handle, CURLOPT_URL,$url);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
$fileContent = curl_exec($curl_handle);
curl_close($curl_handle);
This command generates the following Error (I replaced the Event ID with XYZ) :
Array
(
[error] => Array
(
[code] => 400
[message] => Invalid JSON payload received. Unknown name "{"command":"sdm.devices.commands.CameraLiveStream.GenerateRtspStream","params":{"eventId":"XYZ...."}}": Cannot bind query parameter. Field '{"command":"sdm' could not be found in request message.
[status] => INVALID_ARGUMENT
[details] => Array
(
[0] => Array
(
[#type] => type.googleapis.com/google.rpc.BadRequest
[fieldViolations] => Array
(
[0] => Array
(
[description] => Invalid JSON payload received. Unknown name "{"command":"sdm.devices.commands.CameraLiveStream.GenerateRtspStream","params":{"eventId":"XYZ..."}}": Cannot bind query parameter. Field '{"command":"sdm' could not be found in request message.
)
)
)
)
)
)
I have done the following Test :
Google provides a Terminal function which allows me to enter the original syntax described in the Documentation (see above) without relying on PHP Curl.
After a successful manual Test I used my PHP code to create the RAW Data :
$sub ['eventId'] = $event_id;
$PostData_array = array('command' => "sdm.devices.commands.CameraLiveStream.GenerateRtspStream" ,
'params' => $sub
);
$PostData = json_encode ($PostData_array);
and the URL :
$url = 'https://smartdevicemanagement.googleapis.com/v1/enterprises/'.$project_id.'/devices/'.$device_id.':executeCommand';
I manually copied the strings into this structure :
curl -X POST \
'' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer ' \
--data-raw ''
I then copy paste the following code into the Terminal and it worked (sensitive Info deleted) :
curl -X POST \
'https://smartdevicemanagement.googleapis.com/v1/enterprises/f779e361..../devices/AVP.......:executeCommand' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer ya29........' \
--data-raw '{"command":"sdm.devices.commands.CameraLiveStream.GenerateRtspStream","params":{"eventId":"Ci........."}}'
So the error must originate from my Curl Code :
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('Authorization: Bearer '.$access_token ));
curl_setopt($curl_handle, CURLOPT_POST, true);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $PostData);
curl_setopt($curl_handle, CURLOPT_URL,$url);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
$fileContent = curl_exec($curl_handle);
curl_close($curl_handle);
OK Problem is fixed. My Headers where wrong. The correct Curl Code is this :
$curl_handle=curl_init();
$headers = array(
"POST HTTP/1.0",
"Content-type: application/json",
"Accept: application/json",
"Content-length: ".strlen($PostData),
"Authorization: Bearer ".$access_token
);
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl_handle, CURLOPT_POST, true);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $PostData);
curl_setopt($curl_handle, CURLOPT_URL,$url);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
$fileContent = curl_exec($curl_handle);
curl_close($curl_handle);
Tks everybody for your response, it helped me to move in the correct direction
UPDATE*
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $_ENV["https://cf6307f08afef7f0f9f449a55c6fd79b#api.dialmycalls.com/2.0/service/text"]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = array (
'name' => 'Dorothy',
'keyword_id' => '351aa984-9a7b-11e8-a4d5-0cc47ab3cb58',
'messages' => 'test123456',
array ("contacts" => array(
array(
"phone" => "12294622255"
))));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
?>
I am still getting
`Error: malformed`
So maybe a little more information about the json construction might help
These are the required fields
name - > name of broadcast
keyword_id - > 351aa984-9a7b-11e8-a4d5-0cc47ab3cb58
messages - > list format but only sending single message "test123456"
contacts - > List format with substring of phone: then number 1234567891
ORIGINAL QUESTION
So i am trying to setup a php page with DialMyCall's API just to sent text with the variables
$numbers (Command Delimited)
$message (message of SMS)
The example that DialMyCode gives is
curl -i -H "Content-Type: application/json" -X POST -d "{\"keyword_id\": \"dfe49537-a0a8-4f4a-98a1-e03df388af11\", \"send_immediately\": true,\"messages\": [\"Testing testing\"], \"contacts\": [{\"phone\":\"1116551235\"},{\"phone\":\"1116551234\"}]}" https://$API_KEY#api.dialmycalls.com/2.0/service/text
I have tried to convert this into php but i cannot get it to work
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $_ENV["https://APIKEY#api.dialmycalls.com/2.0/service/text"]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array (
'name' => 'Dorothy',
'keyword_id' => '351aa984-9a7b-11e8-a4d5-0cc47ab3cb58',
'message' => 'test123456',
'contacts' => 'phone: 1234567891',
));
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
?>
Error recieved
Error: malformed
What i should get is a success message in JSON and a text message sent.
You need to send your data as a JSON string. When you pass an array to CURLOPT_POSTFIELDS, it formats/passes it like form data. Here, you want to pass a json object as the body, so use something like this below:
$data = array (
'name' => 'Dorothy',
'keyword_id' => '351aa984-9a7b-11e8-a4d5-0cc47ab3cb58',
'messages' => array('test123456'),
'contacts' => array(
array('phone' => '1234567891')
)
);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
I have been trying to get the list of subscribers from drip account. I am trying to do so with the curl php I am unable to do so.
Official example
curl -H 'User-Agent: Your App Name (www.yourapp.com)' \
-u f4ff6a200e850131dca1040cce1ee51a: \
-d status=active \
https://api.getdrip.com/v2/9999999/campaigns
My Code
$TOKEN='f69444e104aea5b77a969bb313852dc1';
$ch = curl_init('https://api.getdrip.com/v2/1186104/subscribers');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'TestApp (laflechee#gmail.com)');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer ' . $TOKEN
));
$data = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
echo $data;
I am adding a new subscriber in drip with the below code. You can follow it.
$ch = curl_init();
//YOUR-ACCOUNT-ID replace it with your account id
curl_setopt($ch, CURLOPT_URL, 'https://api.getdrip.com/v2/YOUR-ACCOUNT-ID/subscribers');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$data = array(
'subscribers' => array(
array(
'email' => 'myemail#domain.com',
'first_name' => 'Mohsin',
'last_name' => 'raza'
),
),
);
$post = json_encode($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
//YOUR-API-TOKEN-HERE replace it with your api-token
curl_setopt($ch, CURLOPT_USERPWD, 'YOUR-API-TOKEN-HERE' . ':' . '');
$headers = array();
//replace with your app name and registered domain with drip account
$headers[] = 'User-Agent: YOUR-APP-NAME (www.your-domain.com)';
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
echo $result;
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
I have went through steps of getting an access token ( i did this though a terminal using java)
When i try to use a GET request in terminal with java like this:
java -jar rest-oauth-client-1.0.one-jar.jar request ACCESS_TOKEN http://locahost:8015/rest/api/2/issue/TP-113
It works and returns an Issue JSON
But when i try to GET or POST an ISSUE using PHP and cURL like this
$FieldSummary = substr($items[0]->summary,strpos($items[0]->summary,'- ')+2);
$FieldDescription = substr($items[0]->description,0);
$FieldStartTime = new DateTime($items[0]->start->dateTime);
$FieldEndTime = new DateTime($items[0]->end->dateTime);
$dateStart = $FieldStartTime->format("Y-m-d\Th:m:s.sOZ");
$dateEnd = $FieldEndTime->format("Y-m-d\Th:m:s.sOZ");
$url = "http://".$config['jiraAdress']."/rest/api/2/issue/".$FieldKey;
$ch = curl_init($url);
$headers = array(
'Authorization: Bearer ACCESS_TOKEN',
'Accept: application/json',
'Content-Type: application/json'
);
$data = json_encode ( array (
'fields' => array (
'summary' => $FieldSummary,
'description' => $FieldDescription,
'customfield_10007' => $dateStart,
'customfield_10008' => $dateEnd
)
), JSON_PRETTY_PRINT );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
It returns error 401 and error JSON:
{
"errorMessages": [
"You do not have the permission to see the specified issue.",
"Login Required"
],
"errors": {
}
}
What am I doing wrong? Remember that this is JIRA REST API.
GET /post-order/v2/casemanagement/{caseId} call of eBay Post Order API giving an empty string. Can anyone clear out where does my code go wrong? Am I passing the headers correctly? Especially is the syntax of authorization correct?
<?php
error_reporting(E_ALL); // Turn on all errors, warnings and notices for easier debugging
//Seller Authorization Token
$userAuthToken= 'Authorization:TOKEN abc123';
//Endpoint URL to Use
$url = "https://api.ebay.com/post-order/v2/casemanagement/xyz";
//Initializle cURL
$connection = curl_init();
//Set Endpoint URL
curl_setopt($connection,CURLOPT_URL,$url);
//Set HTTP Method
curl_setopt($connection, CURLOPT_HTTPGET, true);
//Create Array of Required Headers
$headers = array();
$headers[] = $userAuthToken;
$headers[] = 'Content-Type:application/json';
$headers[] = 'X-EBAY-C-MARKETPLACE-ID:EBAY-DE';
//var_dump($headers);
//set the headers using the array of headers
curl_setopt($connection,CURLOPT_HTTPHEADER,$headers);
//set it to return the transfer as a string from curl_exec
curl_setopt($connection,CURLOPT_RETURNTRANSFER,1);
//stop CURL from verifying the peer's certificate
curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($connection, CURLOPT_SSL_VERIFYHOST, 0);
//Execute the Request
$response = curl_exec($connection);
//close the connection
curl_close($connection);
var_dump($response);
Try this code it is working fine for me.
<?php
// Your ID and token
$authToken = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
// The data to send to the API
$post_data = json_encode(array("legacyOrderId"=>"110181400870-27973775001"));
$url = 'https://api.sandbox.ebay.com/post-order/v2/cancellation/check_eligibility';
//Setup cURL
$header = array(
'Accept: application/json',
'Authorization: TOKEN '.$authToken,
'Content-Type: application/json',
'X-EBAY-C-MARKETPLACE-ID: EBAY-UK'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
if(curl_errno($ch)){
echo "ERROR:".curl_error($ch);
}
curl_close($ch);
echo json_decode($response,true);
?>
Response:
Array
(
[legacyOrderId] => 110181400870-27973775001
[eligible] => 1
[failureReason] => Array
(
)
[itemEligibilityResult] => Array
(
[0] => Array
(
[itemId] => 110181400870
[transactionId] => 34573775001
[eligible] => 1
[failureReason] => Array
(
)
)
)
[eligibleCancelReason] => Array
(
[0] => OUT_OF_STOCK_OR_CANNOT_FULFILL
[1] => BUYER_CANCEL_OR_ADDRESS_ISSUE
)
)
ADD
curl_setopt($connection, CURLOPT_HEADER, 1);
To return the headers and see what your problem might be.
I also don't know if it matters but my headers array looks like:
$headers = array (
'Authorization: ' . "TOKEN $token",
'Content-Type: ' . 'application/json',
'X-EBAY-C-MARKETPLACE-ID: ' . 'EBAY-US',
'Accept: ' . 'application/json',
);
So you can see there is a space after the colon.