PHP - CURL error when trying to connect with proxy - php

I am trying to connect a website with CURL using a proxy in PHP. But I get "Received HTTP code 400 from proxy after CONNECT" error.
This is the code that I use to connect:
<?PHP
$ch = curl_init("https://ipinfo.io/json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_PROXYTYPE, "HTTP");
curl_setopt($ch, CURLOPT_PROXY, "207.154.231.213:8080");
$output = curl_exec($ch);
if(curl_errno($ch))
echo curl_error($ch);
echo $output;
?>
I don't know what the problem is. The proxy should work perfectly because I check it.

use nex code
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, "https://ipinfo.io/json");
$result=curl_exec($ch);
curl_close($ch);
echo json_decode($result, true);
I recommend http://docs.guzzlephp.org/ library for apis for better practices

Based on the documentation curl_setopt you should either remove your CURLOPT_PROXYTYPE setting (to use the default CURLPROXY_HTTP), or you need to change your invalid setting
curl_setopt($ch, CURLOPT_PROXYTYPE, "HTTP");
to
curl_setopt($ch, CURLOPT_PROXYTYPE, "CURLPROXY_HTTP");
Note that the type HTTP does not exist.

Related

PHP libcurl socks5 not working

I try to connect to socks5 proxy using libcurl, however I receive the answer SOCKS: authentication failed. I also tried to connect to the proxy server through a Powershell script with cURL – it works, so the proxy server is available and my login and password are correct. I tried a lot of solutions... But connection via PHP-script still does not work.
PHP version – 5.4, cURL version – 7.19. What can I do?
[UPDATE] Here is my current code. I want to send message with my Telegram bot :)
$proxy = '<hostname>:<port>';
$proxy_auth = '<username>:<password>';
$url = 'https://api.telegram.org/bot<botID>/sendMessage?'.'chat_id='.$chat_id.'&parse_mode='.$parse_mode.'&text='.'test';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxy_auth);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, true);
$curl_scraped_page = curl_exec($ch);
$error = curl_error($ch);
echo $error;
echo $curl_scraped_page;
curl_close($ch);
Here is the complete answer for anyone else having the same issue.
//Curl setup
$url = "https://google.com";
$proxy = "10.23.2.2:8080";
$password = "username:secret_passowrd";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
curl_setopt($ch, CURLOPT_HEADER, true);
//proxy's address
curl_setopt($ch, CURLOPT_PROXY, $proxy);
//proxy's password
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $password);
//in order to follow redirects.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
//in order to use the result of the query
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//run query
$curl_scraped_page = curl_exec($ch);
$error = curl_error($ch);
echo $error;
echo $curl_scraped_page;
curl_close($ch);
Hope that helps.

Force php to use proxy

I have to tell my php to use proxt settings to allow a connection with website. So how can I force php script to use proxy?
I scraped some HTTP proxy and I want to use them.
I tried adding this to config.php
stream_context_set_default(['http'=>['proxy'=>'101.66.253.22:8080
']]);
I also tried to change function from this:
$ch = curl_init(Site::LINK_LOGIN);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING, "UTF-8");
curl_setopt($ch, CURLOPT_POST, 1);
to this:
$ch = curl_init(Site::LINK_LOGIN);
curl_setopt($ch, CURLOPT_PROXY, '101.66.253.22:8080');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING, "UTF-8");
curl_setopt($ch, CURLOPT_POST, 1);
But none of wchich works properly. Can you help me?
Try to make a new curl_setopt with proxy port.
curl_setopt($ch, CURLOPT_PROXY, "http://proxy.example.com"); //proxy url
curl_setopt($ch, CURLOPT_PROXYPORT, "8080"); // proxy port number
Maybe...
But first check the error message
if(curl_exec($ch) === false)
{
echo 'Curl error: ' . curl_error($ch);
}
else
{
echo 'Operation completed without any errors';
}

How to connect to a secure (https) proxy with curl and php?

Modern browsers support HTTPS proxies that can be connected to via a PAC file (see https://www.igvita.com/2012/06/25/spdy-and-secure-proxy-support-in-google-chrome/ if you're not familiar).
I'm trying to replicate the same thing and connect to such a proxy via php CURL, but I'm simply getting a blank response, no headers or content.
My code is as follows:
$url = "http://checkip.dyndns.com";
$proxy = "proxy.domain.com:443";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL , 1);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'test:test');
curl_setopt($ch, CURLOPT_PROXYTYPE, "CURLPROXY_HTTP");
$response = curl_exec($ch);
curl_close($ch);
echo $response;
Any ideas?
If anyone is interested, the solution to this is to use spdycat instead of curl: https://github.com/tatsuhiro-t/spdylay
There's no support for connecting to a proxy with HTTPS with curl yet. There is a work-in-progress branch in git though: https://github.com/bagder/curl/tree/HTTPS-proxy
We will appreciate help to get that in shape to merge.

consume c# rest web service in php

I am trying to connect to a c# rest web service by php, but when I execute the below code this error happened:
"Message":"The requested resource does not support http method 'GET'".
Could anybody help me about this error?
<?php
$url="https://example/login";
$username='user';
$password='pass';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD,$username':'$password);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$out = curl_exec($ch);
print "error:" . curl_error($ch) . "<br />";
echo $out;
curl_close($ch);
?>
By default, curl is executing the request via HTTP GET. The web service has to be accessed via POST.
Have a look at here for details on how to create a POST request via curl.

cURL over http/sock proxy with authentication

I have problem with scraping data over proxy which use authentication. Proxy is online, its private and it work very fast from browser.
However proxy requires authentication and i just cant think of why this simple script doesnt work?!
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://automation.whatismyip.com/n09230945.asp");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_PROXY, "proxyIP:proxyPORT");
curl_setopt($ch, CURLOPT_PROXYUSERPWD,"username:password");
$result = curl_exec($ch);
curl_close($ch);
return $result;
?>
I have tried to set CURLOPT_HTTPAUTH with any valid value and it is same.. Any suggestion?
Try setting:
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
You need to use CURLOPT_PROXYTYPE to tell curl which type of proxy you're using, as it will otherwise assume default type which is HTTP...
Try this :
curl_setopt($ch, CURLOPT_PROXY, 'proxyIP');
curl_setopt($ch, CURLOPT_PROXYPORT, 'proxyPORT');
instead of this :
curl_setopt($ch, CURLOPT_PROXY, "proxyIP:proxyPORT");

Categories