PHP Curl Posting Code that i made is not working - php

I am using post method and sql query to send sms. So, I am just trying to get this link formet
http://example.com/smsapi/non-masking?api_key=$2y$10$mtW.yfKj18i2mTPe/0iCEuKdCfCGh9zOYYEU9AmnMrJyBb.h7fVcG&smsType=text&mobileNo=(NUMBER)&smsContent=(Message Content)
but my curl code is not working.
$api_key = "$2y$10$mtW.yfKj18i2mTPe/0iCEuKdCfCGh9zOYYEU9AmnMrJyBb.h7fVcG";
$numbers = "$row[mailing_no]";
$message = "Dear Guardian, $row[name] has swiped his card right now";
$type= "text";
$params = array('api_key'=>'$api_key', 'smsType'=>'$type', 'mobileNo'=>'$number', 'smsContent'=>'$message');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://example.com/smsapi/non-masking?".http_build_query($params, "", "&"));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/json", "Accept:application/json"));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
curl_close ($ch);

Just made some improvements to your code, you were using $number on $params, but the variable was named $numbers
$api_key = "$2y$10$mtW.yfKj18i2mTPe/0iCEuKdCfCGh9zOYYEU9AmnMrJyBb.h7fVcG";
$number = $row[mailing_no];
$message = "Dear Guardian, ". $row[name] . " has swiped his card right now";
$type= "text";
$params = array('api_key'=>$api_key, 'smsType'=>$type, 'mobileNo'=>$number, 'smsContent'=>$message);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://example.com/smsapi/non-masking?".http_build_query($params, "", "&"));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/json", "Accept:application/json"));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
curl_close ($ch);

Related

How to Post to googleapis civicinfo using PHP to retrieve JSON data

I'm trying to retrieve CivicInfo data from Google APIs using following PHP code:
$address = $_GET["address"];
$api_key = "[my key]";
$url = "https://civicinfo.googleapis.com/civicinfo/v2/representatives?address=" . $address . "&key=" . $api_key;
$payload = json_encode(array('address' => $address));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result, TRUE);
My code above fails, but I'm not seeing any error. When I do echo $result['status'], nothing is displayed.
Please help!

Send Mailchimp campaign to a specific email address via API v3

I have got the following code but I'm getting a resource not found error.
Has anyone successfully sent an MC campaign to a single user?
$apiKey = 'APIKEYHERE';
$campaignId = 'CAMPAIGNIDHERE';
$memberId = md5(strtolower("test#example.com"));
$dataCenter = 'us19';
$url = 'https://'. $dataCenter . '.api.mailchimp.com/3.0/campaigns/' . $campaignId .'/actions/test';
$jsonEmail = '{}';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, 'apikey:'.$apiKey);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonEmail);
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);

Issue with Strava API

I am trying to get a list of my activities using cURL I can get the list of my clubs ok. Can't seem to figure out what I am doing wrong
I am having an issue when using the request. $ http GET "https://www.strava.com/api/v3/activities/{id}?include_all_efforts=" "Authorization: Bearer [[token]]"
I cam getting an error
string(95) "{"message":"Record Not Found","errors":
[{"resource":"Activity","field":"id","code":"invalid"}]}"
**I have tried the following**
$num = "2149016538";
$int = (int)$num;
// $url = 'https://www.strava.com/api/v3/athlete/clubs';
$url = 'https://www.strava.com/api/v3/activities/{'.$int.'}?include_all_efforts=true';
**No success**
**I have also tried this**
$num = "2149016538";
$int = (int)$num;
// $url = 'https://www.strava.com/api/v3/athlete/clubs';
$url = 'https://www.strava.com/api/v3/activities/?id='.$int.'?include_all_efforts=true';
$authorization = "Authorization: Bearer 23232323";
$num = "2149016538";
$int = (int)$num;
// $url = 'https://www.strava.com/api/v3/athlete/clubs';
$url = 'https://www.strava.com/api/v3/activities/{'.$int.'}?include_all_efforts=true';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization ));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch);
var_dump($result);
curl_close($ch);
json_decode($result);

Coinpayments create_transaction "ERROR: Invalid command!"

I am trying to let people pay on my site with a simple API of Coinpayments (I tought it was simple). So, I found this page of the official Coinpayments website on how to create a transaction and receive money.
So, I am trying to receive the response as JSON en just echo it for now, so I can see what further steps I will take. Now, this is my code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://www.coinpayments.net/index.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"cmd=create_transaction&amount=".$amount."&currency1=USD&currency2=BTC&buyer_email=".$email."&version=1&key=b07f0fee01909b919235d58a950378b8c0e5266fa2174e007b24220a592aa92e&format=json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close ($ch);
When I try to echo the $server_output, it gives this: ERROR: Invalid command!. I have searched around, and I couldn't find anybody having the same issue as I do.
Thanks for the help! <3
This is what works for me:
function curl_coin_payment ($postdata = array()) {
$url = "https://www.coinpayments.net/api.php";
$payload = $postdata;
$payload['key'] = 'public_key';
$payload['version'] = 1;
$payload = http_build_query($payload, '', '&');
$api_secret = 'private_key';
$apiseal = hash_hmac('sha512', ($payload), $api_secret);
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, ($payload)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = [
"HMAC: $apiseal",
"Content-Type: application/x-www-form-urlencoded",
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $request = curl_exec ($ch); curl_close ($ch);
return $request;
}

Sending POST Data via PHP CURL Not Working

Using the code below, I'm unable to send any POST data. The page that is supposed to receive it, isn't receiving it for some reason..
$url = "http://www.somesite.com/curl_test_page.php";
$employee_id = 5;
$post_fields = "employee_id=" . $employee_id;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
$result = curl_exec($ch);
On the curl_test_page.php, the code is as follows:
$employee_id = $_POST['employee_id'];
echo $employee_id;
When I run the CURL script, it displays an empty page.
You are not using curl_init
Is recomended force the enctype with CURLOPT_HTTPHEADER
I recomend you use http_build_query function
Example:
$url = "http://www.somesite.com/curl_test_page.php";
$employee_id = 5;
$post_fields = Array(
employee_id => $employee_id
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_fields));
$result = curl_exec($ch);
You are not printing any output. Try adding
<?php
echo $result;
?>

Categories