PHP CURL Never Ending Request - php

I recently uploaded my project from localhost to a live hosting server. In my project I have used a CURL request, which worked absolutely fine on localhost up til now.
As soon as I moved it on live server (Arizonawebservices.com), that CURL request stopped working. When my page sends the CURL request, it keeps on processing and never returns any output.
I checked server for CURL support (using "which curl" command in PHP's exec() function), and yes it has CURL support enabled.
Also created a test page for manually shooting CURL requests and check if it works ok. Tried www.google.com, www.yahoo.com, and many other URLs, all are working fine, but when I put my original API URL "https://dataviz.sandbox.rcoanalytics.com:10105/oauth/token" in it. It starts behaving like before. I am completely unable to get any response from that CURL request. Any help would be appreciated.
Used following code for testing purpose:
function httpPost($url,$params)
{
$postData = '';
if (!empty($params)) {
//create name value pairs seperated by &
foreach($params as $k => $v)
{
$postData .= $k . '='.$v.'&';
}
$postData = rtrim($postData, '&');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
if ($postData != "") {
curl_setopt($ch, CURLOPT_POST, count($postData));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
}
$output=curl_exec($ch);
if($output === false)
{
echo "Error Number:".curl_errno($ch)."<br>";
echo "Error String:".curl_error($ch);
}
curl_close($ch);
return $output;
}
$params = array(
);
echo httpPost("https://dataviz.sandbox.rcoanalytics.com:10105/oauth/token", $params);

The url has an SSL error, so in the cURL request when cURL tries to verify the host it blocks you from accessing the address.
use the following:
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
This is obviously insecure method to access the URL but for a host like this one it will do the trick.

Related

How to make a call to .aspx https from php script from my localhost with xamp?

I am trying to send SMS from my localhost with xamp installed.
Requested page is on https and an .aspx page.
I am getting error: "HTTP Error 400. The request is badly formed." or blank page only in some cases.
Detaisl is as follows :
$url = 'https://www.ismartsms.net/iBulkSMS/HttpWS/SMSDynamicAPI.aspx';
$postArgs = 'UserId='.$username.
'&Password='.$password.
'&MobileNo='.$destination.
'&Message='.$text.
'&PushDateTime='.$PushDateTime.
'&Lang='.$Lang;
function getSslPage($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$response = getSslPage($all);
echo "<pre>";
print_r($response); exit;
I tried every possible solution/combination found on internet but could not resolve that. The API developers do not have a example for php script.
I tried httpful php library and file_get_contents function but getting empty page. Also tried every combination with curl_setup.
I need to call this url without any post data and see the response from it.
Instead getting a blank page.
Please note that when I execute the url with all details in browser it works fine.
Can anybody help me in this regard.
Thank you,
Usman
First do urlencode over your data as follows:
$postArgs = 'UserId='. urlencode($username.
'&Password='.urlencode($password).
'&MobileNo='.urlencode($destination).
'&Message='.urlencode($text).
'&PushDateTime='.urlencode($PushDateTime).
'&Lang='.urlencode($Lang);
After that two possible solutions. One is using GET.
curl_setopt($ch, CURLOPT_URL, $url . "?" . $postArgs);
Second option is using POST method.
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postArgs);

logging into website using php

Im trying to auto login into a website using php, i dont get any error with this code neither it gives any result to me. here is the code i tried, i get only a blank page, im expecting the sourcecode of the page after login.
<?php
$fields_string=array();
$url = 'https://subscriber.hoovers.com/H/login/login.html';
$fields = array(
"j_username"=>"XXXXXX",
"j_password"=>"XXXX"
);
//url-ify the data for the POST
foreach($fields as $key=>$value)
{
$fields_string .= $key.'='.$value.'&';
}
rtrim($fields_string,'&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields);
//execute post
$result = curl_exec($ch);
echo $result;
//close connection
curl_close($ch);
?>
I was able to get your script to return the sourcecode of the page by adding these curl options:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
By default curl doesn't trust the web server’s certificate, the last option above tells curl not to verify the web server. You might want to switch that last option to this:
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt ($ch, CURLOPT_CAINFO, "path/to/your/cacert.pem");
If you are going to go this route, I would check out Using cURL in PHP to access HTTPS (SSL/TLS) protected sites its a great tutorial and will go over everything you need to know about it.

PHP - How to fire HTTP get request on a secured URL (HTTPS)?

I am trying to fire a HTTP GET request on a secured URL which asks for username and password. This is fine when I am using that from browser but I am not sure how to do that using PHP.
I have tried using the two methods:
1) Using Curl as suggested in here: Make a HTTPS request through PHP and get response
2) Using the file_get_contents as suggested in here: How to send a GET request from PHP?
But the first one didn't give me any response back. And the second one gave me the following error:
failed to open stream: HTTP request failed
And this is my code for the curl:
$url="https://xxxxx.com";
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
and for the file_get_contents:
$url="https://xxxx.com";
$response=file_get_contents($url);
echo $response;
The URL will return a XML response for a API I am testing. Can someone point me to the right direction?
Thanks!
If we focus on the requirement to send a username and password because I suspect that's your main problem, try this
$ch = curl_init();
$url="https://xxxxx.com";
// OR - check with your server's operator
$url="http://xxxxx.com";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// or maybe
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
// - see http://stackoverflow.com/questions/4753648/problems-with-username-or-pass-with-colon-when-setting-curlopt-userpwd
// check the cURL documentation
$output = curl_exec($ch);
$info = curl_getinfo($ch);
// don't forget to check the content of $info, even a print_r($info) is better
// than nothing during debug
curl_close($ch);

cURL on PHP being curiously silent

OK so after struggling for a while trying to get cURL to fetch a password protected XML file, and getting absolutely nothing from it, I tried dumbing down my code to see what would work. It turns out nothing. Here's my code:
$ch = curl_init("www.google.com");
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_exec($ch);
curl_close($ch);
And cURL is SILENT. I mean nothing prints to the page. I definitely have it enabled, my phpinfo looks like this:
cURL support enabled
cURL Information libcurl/7.16.0 OpenSSL/0.9.8a zlib/1.2.3
Running php 5.2 on a Windows NT server. Any suggestions?
The key is in this line:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
You're telling cURL to return the result of the transfer instead of echoing it (the default). So either remove that line, or do something like this:
$result = curl_exec($ch);
if ($result === false) {
echo "Error: " . curl_error($ch);
} else {
echo $result;
}
If you're getting an SSL error as you point out in your comments, you can set another option on the cURL handle to disable checking the certificate:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

grab file from php which forced to save as from another web server php with http authentication

i have a url from another server which having http auth enabled.
this specific url forces file to download a file.
I am trying here to grab the headers and set the headers back so that i can force the download from my php file after http auth done in curl, but no success. curl did not gave any error. nothing returned in $body.
$url=$_GET["url"];//another web server url which forcing file download save as
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERPWD, "username:password");//http auth done here
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, null);
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
list ($headerString, $body) = explode("\r\n\r\n", $response, 2);
$headers = explode("\r\n", $headerString);
foreach ($headers as $header) {
header($header);
}
echo $body;
exit;
code above is working fine. Issue is just & in the $_GET["url"] which splitting valid url. just used urlencode and code working fine now. Hope this will help someone.

Categories