to execute a link using curl - php

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.

Related

PHP cURL request takes up the entire Timeout

I currently have a Laravel application, which is doing a CURL request from one route to another route within the same route. My CURL looks like this:
//LOGGING THAT A CURL CALL IS ABOUT TO BE MADE
$url = env('APP_URL') . '/tests/add/results';
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //return server error
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLINFO_HEADER_OUT, FALSE);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $test_post_data);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$response = curl_exec($ch);
In the route, that the POST is being sent to, the first thing i log, is the request data:
//LOGING THAT I RECEIVED THE CURL CALL in receiving function
I'm noticing, that the logs for the request data get logged exactly the same amount of time as the timeout, meaning the request is actually being sent 10 seconds after the initial call.
In my logs i'll see something like:
10:10:10 - LOGGING CURL CALL
10:10:20 - Recieving CURL call
If i change the timeout to 30, then the log shows 30 seconds later that i received the CURL call.
Does anyone have any idea why this may be happening?
The response from the CURL just comes back as false always.
I did the following to make a post request work:
Instead of calling the route via CURL i did a post directly to the route function
$testController = new TestsController;
$test_data_request = new \Illuminate\Http\Request();
$test_data_request->setMethod('POST');
$test_data_request->request->add( $test_post_data );
$testId = $testController->addTestResults($test_data_request);
You've not provided enough information, but i think, the problem will be one or more of the following:
The WebServer http://127.0.0.1:8000 is not running
The script located on http://127.0.0.1:8000/tests/add/results is running too long and the request timeouts before it is completed
The requested path is returning redirect headers and creates an infinite loop
The response is too big to finish the data transfer in thirty seconds (very wierd if on localhost)
Try some more debugging and provide more information on this, so we may help you.
PS: Firstly i would try to catch the headers (curl_setopt($ch, CURLINFO_HEADER_OUT, true);) and print out the response (var_dump($response);) - or save it to some file :)

Does curl_exec() retry if timeout?

In my application I have to make a POST call to a webservice. They send me an XML response, basically saying "Accepted" or "Refused".
Last week I had an issue with one of these calls: I received a "Refused" response while their backend was telling me this request had been accepted.
I asked them what happened and they told me they received 2 requests (with the same ID - a parameter I send to them). First one was "Refused", second one was "Accepted".
I investigated: in my code, if I receive a "Refused" response, I log it, I update my database, and that's it. I do not try again.
The only thing would be PHP curl functions.
The day the problem occured, the webservice took unusual long time to response (20 seconds).
Could curl have made several calls? There is no retry option in the PHP function (or I didn't find it), but I'd rather ask here to be sure.
Here is my curl code.
$ch = curl_init();
$myArrayWithDatas = array( '...' );
$httpQueryFields = http_build_query($myArrayWithDatas);
$url = "https://www.webservice.com/api";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $httpQueryFields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (empty($response)) {
// I log an error
// (no trace of that in my logs)
} else {
// I log the XML response
// (one "Refused" response logged)
}
curl_close($ch);
Is there any case where this code could send 2 or more requests to the $url?
curl_exec will only do 1 call.
Are you running your code via a cron job or scheduled task ? If that's the case, maybe your code has been launched twice and that would explain why there were two calls done.

How can I output the body of a webpage if the response code is 500 using curl?

I've been through hell and high water with this problem. I get a 500 error on a page a tiny, tiny fraction of the time. I have been completely unable to reproduce it, but Google insists that they see a 500 code. Fetch as Googlebot says it's successful, however something is wrong. I've been down many avenues and the only recourse I have left is to brute-force the local copy of the website.
I want to use curl to hammer the dev site until I get a 500 error, and when I do, to output the body of the page to the terminal so I can actually get some useful information.
for(;;){
$url = "http://www.blahblah.dev/";
$ch = curl_init();
//Set the URL
curl_setopt($ch, CURLOPT_URL, $url);
//Enable curl response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//Enable POST data
curl_setopt($ch, CURLOPT_POST, true);
//Use the $pData array as the POST data
curl_setopt($ch, CURLOPT_POSTFIELDS, $jData);
$result = curl_exec($ch);
if(strstr($result, "error")){
echo $result;
exit();
}
curl_close($ch);
usleep(500000);
}
As you can see, I'm simply checking to see if "error" appears in the body, as I can't figure out how to check for a 500 error properly. I realize that this is a terrible and contrived way of debugging, but it's all I've got at this point. Thanks!

How to test if a remote server is down for maintenance even though there may be a redirect to a maintenance page?

We have an application that is dependent upon a remote service. I have been asked to implement some code whereby if the remote web server is down (due to maintenance or glithes) that I display an appropriate message.
The issue at hand is that when the remote is down for maintenance, they usually redirect to another page. So how do I get about implementing in PHP a robust function that can tell if a particular URL is up and running as opposed to it being redirected to a dummy page.
Thank You
Just check the response text. If the the response contains any text that is present in the redirected url, Then its surely in maintenance mode.
If the remote web server is down you can check it too. see https://stackoverflow.com/questions/9144825/php-check-if-a-site-is-down/9145124#9145124
Just check the HTTP return code. This is possible with curl for instance:
CURLINFO_HTTP_CODE
http://de2.php.net/manual/en/function.curl-getinfo.php
<?php
$success = 0;
// create a new cURL resource
$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);
// Check if any error occured
if(!curl_errno($ch))
{
if(curl_getinfo($c, CURLINFO_HTTP_CODE) === 200)
$success = 1;
}
// close cURL resource, and free up system resources
curl_close($ch);
?>

Simple Issue With cURL

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.

Categories