How to create proxy in PHP - php

I'm trying to create a proxy in PHP. When I visit the page http://example.com everything is OK, but using cURL, I always get a 401 Unauthorized Error.
My code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, getallheaders());
curl_setopt($ch, CURLOPT_URL, 'http://example.com');
print_r(curl_exec($ch));
The response:
HTTP/1.1 401 Unauthorized
Content-Type: text/html
Server: Microsoft-IIS/7.5
WWW-Authenticate: Negotiate
WWW-Authenticate: NTLM
X-Powered-By: ASP.NET
Date: Thu, 20 Oct 2016 01:30:11 GMT
Content-Length: 1293

Related

HTTP/1.1 400 BAD REQUEST Error

My coding is this
$number1= $_POST{'username'};
set_time_limit(0);
error_reporting(0);
$post = array(
'phone_number'=>$number1);
$url="https://www.pokketdeal.com/lokiapp/otp";
$headers[]='Content-Type: application/json';
$headers[]='Content-Length: 31';
// $headers[]='X-Mclient: 3';
// $headers[]='x-wap-profile: http://www.1066.cn/uaprof/prof/Micromax/Micromax_A177.xml';
// $headers[]='X-Requested-With: com.pokkt.app.pocketmoney';
$headers[]='Accept-Encoding: gzip,deflate';
$headers[]='Accept-Language: en-US';
$headers[]='Accept-Charset: utf-8, utf-16, *;q=0.7';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$url");
curl_setopt($ch, CURLOPT_USERAGENT, "okhttp/2.3.0" );
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$html = curl_exec($ch);
echo $html. "<br>";
when submitting data i am getting server response as
HTTP/1.1 400 BAD REQUEST Server: nginx/1.4.6 (Ubuntu) Date: Thu, 10 Dec 2015 12:16:40 GMT Content-Type: text/html Transfer-Encoding: chunked Connection: keep-alive X-Frame-Options: SAMEORIGIN
Bad Request (400)
also my content type is application/json but response is text/html.

get redirect url using curl_getinfo failed

I used below function to get redirected (final) url of these links
http://iprice.my/coupons/zalora/
function curlFileGetContents($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
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, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:16.0) Gecko/20100101 Firefox/16.0');
$result = curl_exec($ch);
$info = curl_getinfo($ch);
$effectiveUrl = $info['url'];
curl_close($ch);
return $effectiveUrl;
}
but I couldn't get anything, I wonder why? Eg I did curlFileGetContents('http://iprice.my/coupons/zalora/#007b9a6024d19edec91d04c2e92e143e744c83b6');
The URL isn't being redirected - the links you are referring to (that only spawn on that page) are pop unders. A look at the curl header shows the URL is not being re-directed:
curl --head http://iprice.my/coupons/zalora/#007b9a6024d19edec91d04c2e92e143e744c83b6
HTTP/1.1 200 OK
Server: nginx/1.4.6 (Ubuntu)
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Vary: Accept-Encoding
X-Powered-By: PHP/5.5.9-1ubuntu4.9
Cache-Control: no-cache
Date: Fri, 22 May 2015 15:06:31 GMT
X-iPrice-Cached: 1
X-iPrice-Cached-Type: Response
The approach you currently have won't work for what you want to do.

CURL HTTP 302 Error

I'm writing a web scraper for a project, which needs to login and save some pages, but after even login, saving the cookie.txt it redirects back to login page. Looks like it's not logging in.
Here is my code:
<?php
$ch = curl_init();
$cookie_file_path = 'cookie.txt';
$cookie_file_path = realpath($cookie_file_path);
$data = array();
$data['txtUser'] = "username";
$data['txtPass'] = "password";
$postStr = "";
foreach($data as $key=>$d){
$postStr .= $key.'='.urlencode($d).'&';
}
$postStr = substr($postStr,0,-1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$agent = $_SERVER["HTTP_USER_AGENT"];
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
//new ones
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_URL,"http://madstore.su/login.php");
curl_setopt($ch,CURLOPT_POST,TRUE);
curl_setopt($ch,CURLOPT_POSTFIELDS,$postStr);
curl_exec ($ch); // execute the curl command
echo 'Curl error: ' . curl_error($ch); //no errrors
curl_close ($ch);
unset($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_URL,"http://madstore.su/index.php");
//new ones
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_exec ($ch);
echo 'Curl error: ' . curl_error($ch);
curl_close ($ch);
?>
I have read about all questions on StackOverflow and searching on Google since hours.
Here is what I'm getting in cookie.txt:
# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.
#HttpOnly_.madstore.su TRUE / FALSE 1577145000 __cfduid d3f365e8218ab84f921e43db0d1500e7c1391327438626
madstore.su FALSE / FALSE 0 PHPSESSID t41g1j9cdl800e9qdj2pq96ef1
Here is what I'm getting as curl error:
HTTP/1.1 302 Found
Server: cloudflare-nginx
Date: Sun, 02 Feb 2014 08:01:40 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.1.6
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
location: login.php
CF-RAY: f655ad243d007e5-LAX
HTTP/1.1 200 OK
Server: cloudflare-nginx
Date: Sun, 02 Feb 2014 08:01:41 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.1.6
CF-RAY: f655ad5141f07e5-LAX
I will highly appreciate if anyone can help me with this issue.
Your approach is wrong. First browse the login page(http://madstore.su/login.php) using curl so that it can store the cookies into the file.
Then do the curl posting and use the cookie that saved earlier. Also you are missing this POST parameter, so add this with your data.
$data['btnLogin'] = "Log in";
When the login is done, then browse the required page using your final curl's GET.

Clickbank API in php?

i need to get digital products from click bank, so am using their API to get products, unfortunately given example code given by them are not working, am using CURL to do this ,
code is below :
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.clickbank.com/rest/1.3/products/list");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/xml", "Authorization:DEV-KEY:API-KEY"));
$result = curl_exec($ch);
curl_close($ch);
print $result;
?>
but i got the below error
HTTP/1.1 400 Bad Request Date: Thu, 21 Feb 2013 05:20:47 GMT Server: Apache/2.2.23 (FreeBSD) mod_jk/1.2.37 mod_ssl/2.2.23 OpenSSL/0.9.8x Vary: Accept-Encoding Connection: close Transfer-Encoding: chunked Content-Type: text/plain The API call (/api/rest/1.3/products/list) requires parameters which are missing : [site]1
have any one got this error before ?
It's expecting a parameter called site. Please see the docs here:
https://api.clickbank.com/rest/1.3/products
Try This:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.clickbank.com/rest/1.3/products/list?site=<your unique affiliate id>");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/xml", "Authorization:DEV-KEY:API-KEY"));
$result = curl_exec($ch);
curl_close($ch);
print $result;
?>

How to list out product details from click bank API and XML?

This is my sample code for click bank API unfortunately this script do not work
Here is the sample code:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.clickbank.com/rest/1.3/products/list");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/xml", "Authorization: DEV:CLERK"));
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
?>
If run this code following error has occur
HTTP/1.1 400 Bad Request Date: Sat, 23 Feb 2013 05:24:10 GMT Server: Apache/2.2.23 (FreeBSD) mod_jk/1.2.37 mod_ssl/2.2.23 OpenSSL/0.9.8x Vary: Accept-Encoding Connection: close Transfer-Encoding: chunked Content-Type: text/plain The API call (/api/rest/1.3/products/list) requires parameters which are missing : [site]1
https://api.clickbank.com/rest/1.3/products
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.clickbank.com/rest/1.2/debug");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/xml", "Authorization: DEV:CLERK"));
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
?>
The required "site" parameter should be the Clickbank ID (also known as vendor).

Categories