PHP: Curl not called URL and got Error No =0 - php

I am developing one SMS application. I have to call one link
for eg http://sendsms.com/send.php?mobile=45455&msg=hello for sending SMS
so I used CURL concept to send sms.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 15, // timeout on connect
CURLOPT_TIMEOUT => 15, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
$err = curl_errno($ch);
$errmsg = curl_error($ch);
$header = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
$output ="Error:".$err." ErrMsg:".$errmsg." Header:". json_encode($header);
The Problem is message is not sending. I checked the SMS API server. And There also no request is received. So the only problem is CURL may not calling the server. I tried to print the Error msg and I got curl_errno($ch) is 0
please provide me the best way to do this

Try to send request by POST:
curl_setopt($ch, CURLOPT_POST ,1);

Related

Get HTTP response body content from site for parsing with PHP

I know there are lots of articles on how to get response with Curl and then decode it but non of them worked. Sending request with CURL. Getting response. Dumping response content on the screen shows only "String(996)". I guess its compressed string. Tried to decompress it but didnt work.
Here is my case if someone can help me with that.
I tried this code with different url and it worked.
<?php
//set up variables
$url = 'https://en.bidfax.info';
$response = get_web_page($url);
var_dump ($response);
function get_web_page($url) {
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_SSL_VERIFYPEER => false // Disabled SSL Cert checks
);
$ch = curl_init($url);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
Went through all posts about curl and gzdecode / encode.

How to get final redirect url in PHP

I have this kind of url
http://www.shareasale.com/r.cfm?u=552661&b=558522&m=51248&afftrack=&urllink=www%2Eericdress%2Ecom
But when I use curl it gives original url as a final redirect URL.
currently I am using below code
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => true, // return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
);
$ch = curl_init($url);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
$err = curl_errno($ch);
$errmsg = curl_error($ch);
$header = curl_getinfo($ch);
curl_close($ch);
return $header['url'];

PHP Curl does not send POST request to server

I have this code:
<?php
$data = array('name' => 'Ross', 'php_master' => true);
$url = 'http://dsaasd.adsds.nl:8081/cops.nsf/orders?openagent';
$html_brand = "www.google.com";
$ch = curl_init();
$options = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 120,
CURLOPT_TIMEOUT => 120,
CURLOPT_MAXREDIRS => 10,
CURLOPT_VERBOSE => true,
);
curl_setopt_array( $ch, $options );
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ( $httpCode != 200 ){
echo "Return code is {$httpCode} \n"
.curl_error($ch);
} else {
echo "<pre>".htmlspecialchars($response)."</pre>";
}
curl_close($ch);
?>
The problem is, if i send curl post to:
$url = 'http://dsaasd.adsds.nl:8081/cops.nsf/orders?openagent';
Then it does NOT work.
But if i send curl post to the same service but on a different server then it works, this is the other server
$url = 'http://dsaasd.adsds.nl:80/cops.nsf/orders?openagent';
I also post data by normal form post to:
$url = 'http://dsaasd.adsds.nl:8081/cops.nsf/orders?openagent';
And then it works and i receive data on the server.
But with this curl post i keep getting:
Return code is 0 Failed to connect to 11.43.45.123: Network is unreachable
Anyone have any idea?
I think you need to add CURLOPT_POST to the $options array. Docs here.

CURLOPT_TIMEOUT not working at all (php)

I am using curl and setting all the parameters correctly (as far as I know) but CURLOPT_TIMEOUT is being ignored and allowing for an infinite loop. Here is the configuration for my Curl Request:
$user_agent = 'Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0';
$options = array(
CURLOPT_CUSTOMREQUEST => "GET", //set request type post or get
CURLOPT_POST => false, //set to GET
CURLOPT_USERAGENT => $user_agent, //set user agent
CURLOPT_COOKIEFILE => dirname(__FILE__)."/cookie.txt", //set cookie file
CURLOPT_COOKIEJAR => dirname(__FILE__)."/cookie.txt", //set cookie jar
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_SSL_VERIFYPEER, FALSE, //ignore ssl
CURLOPT_PROXY =>$proxy['ip'],
CURLOPT_PROXYPORT =>$proxy['port'],
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_AUTOREFERER => true, // set referrer on redirect
CURLOPT_CONNECTTIMEOUT => 20, // timeout on connect
CURLOPT_TIMEOUT => 10, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
);
$ch = curl_init($url);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
$err = curl_errno($ch);
$errmsg = curl_error($ch);
$header = curl_getinfo($ch);
curl_close($ch);
I am not the best at debugging so I'm not sure what the problem could be. Please help me.
The curl_setopt($connection, CURLOPT_TIMEOUT, $seconds) should be called just right before curl_exec() function.
It wasn't working for me when I called it sooner
If your are using CloudFlare note that :
Enterprise customers can increase the 524 timeout up to 6000 seconds
using the proxy_read_timeout API endpoint. If you regularly run HTTP
requests that take over 100 seconds to complete (for example large
data exports), move those processes behind a subdomain not proxied
(grey clouded) in the Cloudflare DNS app.

cURL POST method not working

I am using cURL to call a REST API. And the below code works for GET method , but however for POST method call it doesn't work after i add :
curl_setopt($ch, CURLOPT_POST, true);
What could be the cause ?
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_SSL_VERIFYPEER => false, // Disabled SSL Cert checks
CURLOPT_HTTPHEADER => array('appId: C579929D-F0AA-4A8F-B6C6-1FF96694483C','appKey: AE78E7F1-FBDE-45F5-BAC2-210CEE9D3ED9')
);
$url = "some-url";
$ch = curl_init($url);
curl_setopt_array($ch, $options);
curl_setopt($ch, CURLOPT_POST, true);
$output = curl_exec($ch);
echo $output;
P.S
I double checked the API call using PostMan and it returns the expected result.
Do i need to add additional properties to API call ?
And i have to call REST API of format :
https://api.abc.com/v1/transactions/companyid/123/userid/456/description/ppeck/‌​source/PPA
And as suggested in the answer i have updated my code as :
$url = "https://api.abc.com/v1/transactions";
$ch = curl_init($url);
curl_setopt_array($ch, $options);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "companyid=12301&userid=31401&description=ppa-check&source=PPA&amount=123&txndate=010111&txntypeid=420&customerid=2701");
$output = curl_exec($ch);
And sadly it didn't worked.
try
post fields must be as a query string with appending (&) like
$url = 'http://example.com'; //your api call url
$fields = 'q=example&y=fgt&ghty'; // all posting fields separated with &
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);

Categories