How to prevent Curl from redirecting [duplicate] - php

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
PHP: Can CURL follow meta redirects
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http.....');
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_HEADER,1);
curl_setopt($ch, CURLINFO_HEADER_OUT,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch,CURLOPT_AUTOREFERER,0);
curl_setopt($ch,CURLOPT_POSTFIELDS,"vb_login_username=***&vb_login_password=***&do=login");
curl_exec($ch);
curl_close($ch);
?>
I want to login to a vbuletin forum. This login script works (it show the login success dialog), but after seconds, it redirects to the index page.
I knew the problem, that is:
<META HTTP-EQUIV="refresh" CONTENT="seconds;URL=the-other-url">`
I need a solution to stop this :D

You can use
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
and you can get response $response = curl_exec($ch);

Related

Creating an Array out of Json Data in PHP (Google API) [duplicate]

This question already has answers here:
php: loop through json array
(4 answers)
Closed 4 years ago.
I have a small issue that is driving me nuts.
Through a GET Request, I obtain a nice amount of json Data from google.
How in the world can I loop through it, in order to get only the ['videoID'] entries.
I would be really glad if someone could help me out on this.
$url ="https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=UUyrB0KeB5pSGPajE0RVMoRA&key=AIzaSyDaLp92MtlcSnJjFVZYjoZIs7z5Wi_P-gQ";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Disable SSL verification
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
$json = json_decode($result,true);
print_r($json);
Please use below code:
$url ="https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=UUyrB0KeB5pSGPajE0RVMoRA&key=AIzaSyDaLp92MtlcSnJjFVZYjoZIs7z5Wi_P-gQ";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Disable SSL verification
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
$json = json_decode($result,true);
$videoIds = array();
foreach($json['items'] as $item){
$videoIds[] = $item['snippet']['resourceId']['videoId'];
}
print_r($videoIds);
Hope this helps.

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

curl will not redirect

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

cURL Login on Website

here´s my problem:
I´m trying to get the content of a website that first needs a login. I wanted to solve this via cURL.
First I´m connecting to the Login-Page, than to a page that requires the login.
I get some Cookies in my cookie file back, but when I try to see the content of the page that requires a login before, I only get redirected to (get the content of) the login page.
Seems that parsing my login cookie or whatever fails, so the website don´t remeber that I logged in.
Heres is my php-Code so far:
<?php
$loginUrl = 'https://www.****./login.html';
$loginFields = array('j_username'=>'***', 'j_password'=>'**'); //login form field names and values
$remotePageUrl = 'https://www.***/myPage/index.html'; //url of the page I want the content
$login = getUrl($loginUrl, 'post', $loginFields); //login to the site
echo $remotePage = getUrl($remotePageUrl); //get the remote page
function getUrl($url, $method='', $vars='') {
$ch = curl_init();
if ($method == 'post') {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
}
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'C:\\xampp\htdocs\***\cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'C:\\xampp\htdocs\****\cookies.txt');
$buffer = curl_exec($ch);
if (curl_error($ch)) echo curl_error($ch);
curl_close($ch);
return $buffer;
}
?>
Any ideas? Searched now for hours in the web, and didn´t find a solution yet.
Thanks!
I did not find any solution. Nevertheless I did all the work manually and don´t need the programm anymore, anyway thanks for the comments :)

get url content PHP [duplicate]

This question already has answers here:
Get file content from URL?
(5 answers)
Closed 4 years ago.
I wanna put the content of a URL in a string and the process it. However, I have a problem.
I get this error:
Warning: file_get_contents(http://www.findchips.com/avail?part=74ls244) [function.file-get-contents]: failed to open stream: Redirection limit reached,
I have heard this comes due to page protection and headers, cookies and stuff.
How can I override it?
I also have tried alternatives such as fread along with fopen but I guess I just don't know how to do this.
Can anyone help me please?
original answer moved to this topic .
Use cURL,
Check if you have it via phpinfo();
And for the code:
function getHtml($url, $post = null) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
if(!empty($post)) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
Try using cURL instead. cURL implements a cookie jar, while file_get_contents doesn't.

Categories