Simple Issue With cURL - php

i am in the closing stages of an sms application that will send different sms messages to different phone numbers. I will be using an sms gateway and my research led me to use cURL to implement the smpp api of the company that has the gateway. I was doing a test run of cURL on my localhost to see if all went smoothly before i implemented on the application(i wanted it to insert values into a table), but it was not working. So i need help on where i am doing something wrong.Here is my code. Thanks.
include 'sms_connect.php';
$sql="select name from sms";
$result=mysqli_query($link,$sql);
while($row=mysqli_fetch_assoc($result))
{
$name=$row['name'];
$url = "http://localhost/sms/index.php?name=".$name;
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
curl_exec($ch);
}
// close cURL resource, and free up system resources
curl_close($ch);

Do
$results = $curl_exec($ch);
echo $results;
Look at the output. If you get the output you expect, the problem is when you insert the data in the database, if not - you have a problem with your cURL, and post the error message here.

Related

Twilio: Accessing IBM Watson Speech-to-text results payload

New Twilio developer here. My app uses the IBM Watson Speech-to-text Add-on, but I'm having trouble accessing the results payload in my callback. I can't find helpful documentation or any discussion of the issue in forums.
What I know/What I've tried
The payload resource exists – I'm able to access it directly via browser.
Using the syntax prescribed by the Twilio PHP helper library client returns a 61005 "Bad request" error:
$request = $client->v1->lookups
->phoneNumbers("+1XXXXXXXXXX")
->fetch(
array(
"AddOns" => "ibm_watson_speechtotext",
));
Using cURL to get the resource directly has been equally unfruitful, returning an empty string.
$request = json_decode($_REQUEST['AddOns']);
error_log("URL: ".$request->results->ibm_watson_speechtotext->payload[0]->url);
$ch = curl_init($request->results->ibm_watson_speechtotext->payload[0]->url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$account_sid:$token");
$json = curl_exec($ch);
if($json === false) {
error_log("cURL error: ".curl_error($ch));
error_log(print_r($json,true));
}
curl_close($ch);
$obj = json_decode($json);
Any recommendations?
The following resources should help you find the results you're looking for.
Your first code snippet above doesn't apply (Lookup is a different product).
instead you will want to use the add-on results api to grab the results.
https://www.twilio.com/docs/api/add-ons/results-api
For your second snippet, you will need to enable follow redirect option with CURL.
Clients will need to follow the redirect to receive the data
associated with a Payload resource.
These may also help as you explore add-ons:
https://www.twilio.com/docs/api/add-ons/using-add-ons#add-on-results-available-callback
and
https://www.twilio.com/docs/guides/voice/how-to-use-recordings-add-ons-in-python

to execute a link using curl

i am trying to execute a link (without page being redirected) using curl.
see below my code...
$ch = curl_init(); // Initializing
//curl_setopt($ch, CURLOPT_URL, trim("http://api.smsgatewayhub.com/smsapi/pushsms.aspx?user=stthomasmtc&pwd=429944&to=9176411081&sid=STMTSC&msg=Dear Sam,%20choir%20practice%20will%20be%20held%20in%20our%20Church%20on%20July%2031%20at%208:00%20pm.%20Thanks,%20St.%20Thomas%20MTC!&fl=0&gwid=2")); // Set URI
curl_setopt($ch, CURLOPT_URL,"http://api.smsgatewayhub.com/smsapi/pushsms.aspx?user=stthomasmtc&pwd=429944&to=9176411081&sid=STMTSC&msg=Dear Sam,%20choir%20practice%20will%20be%20held%20in%20our%20Church%20on%20July%2031%20at%208:00%20pm.%20Thanks,%20St.%20Thomas%20MTC!&fl=0&gwid=2"); // Set URI
curl_setopt($ch, CURLOPT_HEADER, 0); //Set Header
curl_setopt($ch, CURLOPT_TIMEOUT, 300); // Time-out in seconds
$result = curl_exec($ch); // Executing
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode != 200) {
$result = ""; // Executing
}
curl_close($ch); // Closing the channel
return $result;
but i get the response as Bad Request.
when i try to change the url to www.google.com, it seems executing. When i manually use this link in browser, its executed as expected - to send message to me. let me know if there is a better way to execute a link without page being redirected...
This maybe a very old question. But since it has gone unanswered and pops up in Google when you say SMSGatewayHub + SO, I'll go ahead and present an alternative.
Get the class https://github.com/adarshdec23/SMSGatewayhub. It works with both promotional and transactional SMS messages.
Here is a step by step guide to using it.
It uses an API key instead of your username and password. Once you're done with that, the class makes an API call (without cURL). Its simple and gets the job done.

connecting to volusion store using curl functions

I have volusion store URL and I am connecting to volusion store using this URL and curl function. Following is my code to connect -
$URL
='http://v970032.y5pgm9yfhypo.demo17.volusion.com/net/WebService.aspx?Login=rashmi#edreamz.in&EncryptedPassword=24EA69124482A486AF3E6BA68DDEECBB7CBC3661EA792E0DC4A40CD6FC031E6E&Import=Update';
$ch = curl_init($URL);
I just want to check whether the connection for the given URL is connected or not. How can I achieve this please advise me. Right now if I used -
echo $ch
it is only displaying Resource id #2
thanks in advance.
First, curl_init ins't enough. You'll need to use curl_exec to actually send the request. Once the request is sent, you can use curl_errno to check if an error was returned and curl_getinfo to get more information about the request and response such as the HTTP response code (e.g.: 200, 404), how long it took to connect, etc.
Modified example from the curl_getinfo documentation:
<?php
// Create a curl handle
$ch = curl_init('http://v970032.y5pgm9yfhypo.demo17.volusion.com/net/WebService.aspx?Login=rashmi#edreamz.in&EncryptedPassword=24EA69124482A486AF3E6BA68DDEECBB7CBC3661EA792E0DC4A40CD6FC031E6E&Import=Update');
// Tell cURL to return the transfer instead of outputting it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute
$result = curl_exec($ch);
// Check if any error occured
if (!curl_errno($ch)) {
// Here, I'm outputting the value of $result but you can do whatever you'd like
// with it such as using a DOMDocument to parse XML or HTML, json_decode to
// parse JSON, etc.
echo $result;
} else {
echo 'Sorry, an error occurred while connecting to Volusion.';
}
// Close handle
curl_close($ch);
Depending on what you're trying to do, you might want to have a look at curl_setopt and more specifically at CURLOPT_RETURNTRANSFER and some of the other basic options you can pass to cURL. The options are well documented.

Header() substitute

Hi I am new to php and want to know some alternate function for the header('location:mysit.php');
I am in a scenario that I am sending the request like this:
header('Location: http://localhost/(some external site).php'&?var='test')
something like this but what I wanna do is that I want to send values of variables to the external site but I actually dont want that page to pop out.
I mean variables should be sent to some external site/page but on screen I want to be redirected to my login page. But seemingly I dont know any alternative please guide me. Thx.
You are searching for PHP cUrl:
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
Set the location header to the place you actually want to redirect the browser to and use something like cURL to make an HTTP request to the remote site.
The way you usually would do that is by sending those parameters by cURL, parse the return values and use them however you need.
By using cURL you can pass POST and GET variables to any URL.
Like so:
$ch = curl_init('http://example.org/?aVariable=theValue');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
Now, in $result you have the response from the URL passed to curl_init().
If you need to post data, the code needs a little more:
$ch = curl_init('http://example.org/page_to_post_to.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'variable1=value1&variable2=value2');
$result = curl_exec($ch);
curl_close($ch);
Again, the result from your POST reqeust is saved to $result.
You could connect to another URL in the background in numerous ways. There's cURL ( http://php.net/curl - already mentioned here in previous comments ), there's fopen ( http://php.net/manual/en/function.fopen.php ), there's fsockopen ( http://php.net/manual/en/function.fsockopen.php - little more advanced )

Integration of escrow web services in a PHP site

I want to integrate the services of escrow.com in my PHP site.
How would you get started with this goal, and what APIs provided would be the basic functionality? Do you have any PHP specific advice or gotchas? Would you recommend another service provider?
I'm working on an API project with this Company at the moment. I know looking at the documentation it all looks a little daunting, however, you can get away with making it as simple as a small cURL request.
I'd suggest starting with the "New escrow transaction" example provided, and build your request using the provided XML they offer, amended with your details.
Assign the XML to a variable, and pass it through a curl request similar to the below;
// Initialise your cUrl object
$ch = curl_init('https://xml.Escrow.com/Invoke/Partners/ProcessrequestXML.asp');
//set your cURL options
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "\$xmldata=".urlencode($xml));
//Start your cURL Transaction
ob_start();
//execute your cURL object with your parameters
$result = curl_exec($ch);
//set the returned info to a variable
$info = curl_getinfo($ch);
// close the transaction
curl_close ($ch);
//get the contents of the transaction
$data = ob_get_contents();
ob_end_clean();
//optional; Redirect to a specific place
header("Location:".$url);
The only advise I can offer is to read through the documentation carefully, and always check the values you are passing in.
Where possible, it is also a good idea to segregate the API functions into their own class, this will make maintenance and troubleshooting, as well as testing the functionality that much easier.
This is the first time I hear about escrow, but a quick scan of the site gives me:
this contact form to get more info:
https://escrow.com/contact/sales.asp
A FAQ:
https://www.escrow.com/support/faq/index.asp?sid=8
www.Transpact.com offers a similar but lower cost service.
It is also UK Government (FSA and HMRC) registered.
It offers a simple SOAP API for easy integration into your website.

Categories