curl will not redirect - php

Please excuse the probable simplicity of the question but I am new to curl and therefore finding my feet. I have the following which is in a php page and when called meant to post dept=xxx to mysite
$ch = curl_init( );
curl_setopt($ch, CURLOPT_URL,"http://mysite/");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "dept=".$departmentName);
curl_exec ($ch);
curl_close ($ch);
I have 2 queries:
1) When called the screen goes blank and the user is not redirected.
2) If redirection is correct, am I right in thinking that the following will correctly receive the data posted:
if (isset($_POST["dept"]))
{
$deptName=$_POST["dept"];
}

Include this cURL paramter to your existing set.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

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);

How to get value of variable from curl in PHP?

I am working on two system.In which asterisk runs on one system-1.I want to run command in asterisk and get result back in system-2.I make curl request like below.How to get value back on system2?enter code here
exec('asterisk -rx "sip show peers"',$sip);
$POST_DATA = array(
'filename'=>$sip,
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,'http://192.168.50.138/test.php');
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $POST_DATA);
$response = curl_exec($curl);
curl_close ($curl);
?>
Since you already have
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
in your code. curl_exec should already returns the content of the page instead of a BOOL.
This is a snippet of a library I use. As pointed out this might not be needed but it helped me out once...
//The content - if true, will not download the contents
curl_setopt($ch, CURLOPT_NOBODY, false);
Also it seems to have some bugs related to CURLOPT_NOBODY (which might explain why you have this issue):
http://osdir.com/ml/web.curl.general/2005-07/msg00073.html
http://curl.haxx.se/mail/curlphp-2008-03/0072.html

cURL stristr errors

I'm trying to figure out why this won't work for me. I'm a complete noob when it comes to cURL, today is my first day using it. I followed a tutorial for this but obviously failed.
It should check the page and if it sees "Skill Stats" on there, then return "Success", and return "Failure" if it spots "Member Rankings".
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://services.runescape.com/m=hiscore/compare.ws?user1=Mercon185");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
curl_setopt($ch, CURLOPT_POST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
if (stristr($output,"Skill Stats")) {
echo 'Success';
}
if (stristr($output,"Member Rankings")) {
echo 'Failure';
}
curl_close($ch);
?>
`
You need to enable follow redirects. As I see currently, your URL redirects to http://services.runescape.com/m=hiscore/overall.ws?errorcode=1. Without follow redirects enabled, it only fetches the first page, which indeed is empty.
The final landing page though, contains the data you want, so if you add this line to your cURL options, it should work:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

get URL Answer with curl or json

I need to get the answer of a page.
The url looks like this:
sp2.looki.de/index.php?page=newsysview&cid=48713&ppx=71&ppy=32&cid=48713&tcv=1355771664807&_=_585_204
The Answer I get looks like this:
{"module":"system","error":[],"syslist":{"15":{"con1":"0","con2":"0","kgm":"257506","kgk":"317370","kgt":"10300255","ppx":"71","ppy":"32","ppz":"15","pname":"Ckaleme","playerid":"5428","flag":"1","noob":"85315748","sperrflag":"-1","nick":"S7alker","tag":"-R-","pid":"707","allianzid":"707","inaktiv":1,"platz0":"82","punkte0":"187044480","platz1":"196","punkte1":"21326785","platz2":"87","punkte2":"105724483","platz3":"69","punkte3":"59993212","oldlogin":null,"nickdays":"0","isnoob":false}},"tflist":[],"ppx":71,"ppy":32,"allianzid":3225,"allianzpid":3225,"debug":{"parsetime":[{"name":"Start","parsetime_complete":"0.000","parsetime_last":"0.000"},{"name":"Ende","parsetime_complete":"0.014","parsetime_last":"0.014"}],"parsetime_total":"0.014","querytime":0.0026}}
I've tried with CURL, file_get_contents and so on ... but the answer was just an
www:redirect
Code edited....no result
$data = "http://some.site.de/index.php?page=newsysview&cid=48713&ppx=50&ppy=50&cid=48713&tcv=1355426935816&_=_552_140";
$ch = curl_init($data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
The Page --> http://sp2.looki.de/index.php?page=newsysview&cid=48713&ppx=50&ppy=50&cid=48713&tcv=1355511915397&_=_482_292
I get a json as answer
So ... after some days of tryin' I have no ideas anymore.
No idea, how to login there, to jump to a specified page and read the json from there.
maybe someone has an great idea to help me out.
LogIn Page is here --> http://sp2.looki.de/
:'(
Edit 2
I stuck ....
I've the following code now ...
$data1 = "http://sp2.looki.de/index.php?page=newsysview&cid=48713&ppx=50&ppy=50&cid=48713&tcv=1355511915397&_=_482_292";
$ch = curl_init ($data1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = curl_exec ($ch);
in my var_dump($data) it's just an {"www-redirect": "/"}
WHY?
oh .... hint:
the original address is: http://sp2.looki.de/index.php?page=gui&cid=666#nothing
the address in $data1 seems to be an ajax request.
Ensure you have Follow Location active in your curl request:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
Otherwise you are forcing it to not follow the "Location:" headers
http://php.net/manual/es/function.curl-setopt.php
Additionally, maybe the website doesn't allow direct query of that URL, try to trick it using the "Referer" value of curl
curl_setopt($ch, CURLOPT_REFERER, 'http://some.site.de/');

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);

Categories