curl exec 504 Gateway Time-out - php

My curl PHP code:
$url = "";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
If $url is, for example, https://www.google.com then all is OK.
But if $url is, for example, https://www.twitch.tv I cannot get answer, just getting:
504 error
What can be wrong?

It is probably due to the redirection to the https site. Try using the url https://www.twitch.tv.

It could your ip is Blacklisted try to use proxy and set SSL_VERIFYPEER to false :
$url = "https://www.twitch.tv";
$proxy='xxx.xxx.xxx.xxx:xxxx';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_PROXY,$proxy);
curl_setopt($ch, CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$answer = curl_exec($ch);
print $answer;

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.

Make PHP Request to tomcat localhost using cURL and get response

I'm new to PHP and am currently struggling to set this up. Could someone provide me an outline of how you use curl to post and retrieve the returning data?
I tried this out:
$url = 'http://localhost:8080/ds/stuff?maybe=false';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, $string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
But I keep getting the HTTP status 405 Error
(aka this):
Am I doing something wrong/what should I do? Or do I have to change something in my ds/stuff
You should set CURLOPT_POST to true. POST data goes to CURLOPT_POSTFIELDS.
$url = 'http://localhost:8080/ds/stuff?maybe=false';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

How do I obtain the URL that a cURL request returns?

Trying to understand the Deezer API.
When I visit:
http://connect.deezer.com/oauth/auth.php?app_id=MY_APP_ID&redirect_uri=http://mydomain.me&perms=basic_access
I end up at
http://mydomain.me/?code=8b69a9a6137094814375bcd5aa9a6c02
I'd like to parse the code variable out of the response.
How can I do this with cURL?
Here's what I've tried:
$url = 'http://connect.deezer.com/oauth/auth.php?app_id=MY_APP_ID&redirect_uri=http://mydomain.me&perms=basic_access';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$a = curl_exec($ch);
$a = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
echo "<pre>";
var_dump($a);
die();
You have errors in your code
These 2 lines should have $ch for the curl handle
curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($c, CURLOPT_HEADER, true);
The link you posted redirects to deezer.com/soon.php.

How to know the Proxy is working in curl/php

How to know that the proxy is working or not in curlphp
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT, 0);
curl_setopt($ch, CURLOPT_PROXYPORT,8080);
curl_setopt($ch, CURLOPT_PROXYTYPE, 'HTTP');
curl_setopt($ch, CURLOPT_PROXY,'218.213.90.92');
$head = curl_exec($ch);
curl_close($ch);
You can set your $url to "https://soulogic.com/ip/"
If return ::ffff:218.213.90.92, it means proxy is working.
Or on your server side, make a PHP file wroten:
<?php
echo $_SERVER["REMOTE_ADDR"];

Simple PHP Google Places API request

So if I put this in my browser URL:
https://maps.googleapis.com/maps/api/place/search/xml?location=45.508867,-73.554242&radius=500&sensor=false&key=mykey
It works. But if I try the exact same call in PHP I get ACCESS DENIED:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://maps.googleapis.com/maps/api/place/search/xml");
curl_setopt($ch, CURLOPT_POSTFIELDS, "location=45.508867,-73.554242&radius=500&sensor=false&key=mykey");
$result = curl_exec($ch);
echo $result;
?>
I tried many different options and minor changes but nothing works and I can't figure out why.
If you're doing this for testing purposes, make sure SSL verification is not enabled, as it will prevent it may prevent the request form being executed.
I believe you're missing this curl setting:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$latitude = 27.7172;
$longitude = 85.3240;
$radius = 50000;
$name = "kupondole";
$url = "https://maps.googleapis.com/maps/api/place/search/json?name=".urlencode($name)."&location=".$latitude.",".$longitude."&radius=".$radius."&key=MY_KEY";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
Works perfectly fine.

Categories