Different cURL behaviour on Ubuntu and Windows - php

I'm using cURL to make some requests to external API. Everything works fine, I get some token with first request to use it later on to do stuff which require authorization header.
I add http header with function:
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeader);
And here's the problem. On Ubuntu environment I get correct response. On Windows - request time out (which probably is their API setting to not send response on wrong data). On both environment I have same headers, but different results.
Is there some environment specific settings that could make Windows request incorrect?

You can try setting value for CURLOPT_CONNECTTIMEOUT. Set it to 0 to wait indefinitely to connect to the server, or any other value in seconds.
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30) //wait for 30 sec.
Try this, maybe this works for you.

Related

Print sent php curl request

How can I print request I have sent, header and body? Following:
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
gives me just $r['request_headers']?
i made a server & website specifically for this purpose, http://dumpinput.ratma.net for the website, and https://github.com/divinity76/dumpinput.ratma.net for the server, note that it does not support httpS, so if the input is sensitive, you should probably run your own instance of the server (it's free & open source~), also note that i run the server on a dev-vm from cloudatcost.com, which has a bad reputation for uptime, so don't depend on the website being up.
you could also set up a netcat server as the target of the curl request (but that won't work with Expect 100 Continue POST-requests, but the dumpinput server will still work.)

Accessing googleapis fails

My user logs into my app, app sends idtoken to my server, server sends request to google, google sends userdata to my server, server puts it in my db. worked fine for 5 Months.
Now:
If I type in my webbrowser
https://www.googleapis.com/oauth2/v3/tokeninfo?id_token= (+ idtoken of my google account extracted from my app)
I'll get the json with my name and so on in milliseconds as it should be.
today, since 6 hours, if my phpfile runs the command
$url = "https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=".$idtoken;
$str =file_get_contents($url);
The page is loading and loading and finally returns no value.
I tried this version to get the .json file
$ch = curl_init($url); // such as http://example.com/example.xml
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$str= curl_exec($ch);
curl_close($ch);
no fix with that.
I cannot install composer because no root access.
I contacted customer support, they told me everything is fine on the serverside.
If after checking on the server's console that you can't curl to it over SSL, but can otherwise, it would suggest the port is blocked. Ask the hosting provider to open it.

Curl hangs my apache server when doing POST to https

I have this very weird issue with curl, when I do a post request to my ubuntu apache server the apache server hangs, if I debug my php code I can see that
$fp = curl_exec($ch);
Never returns. I think the actual post request is correct because I can successfully do the same POST request with postman.
Below are my curl options :
$ch = curl_init();
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER , 0);
curl_setopt($ch,CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_HTTPHEADER,$testHeader);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post_data);
$fp = curl_exec($ch);
One weird thing is that if im quick, and reload my apache server
sudo service apache reload
while its in this hanged state the curl_exec immediately returns and I get my data. Im not a real web developer and more of a windows guy so any help would be greatly appreciated. :)
UPDATE
I can see no error reporting from Curl since the process hangs. It never comes to the point where it can give me the error. When I reload the apache server the request returns and then there are no errors reported. I added the CURLOPT_VERBOSE option.
UPDATE 2:
OK I can also run the post from curl commandline both from the host and the client. Worth mentioning is that my host is a virtual machine. Beginning to think its more of a network thing.
OK I finally solved this stupid error with the help of this thread
PHP curl exec fail on php script same domain
The solution was to add:
session_write_close();
before the curl_exec
and then
session_start();
After. I hope I can help someone by writing it here.

PHP Send Custom HTTP Request

How can I send a custom HTTP Request to a server whose URL is "http://[IP]:[Port]/"?
What I mean is that, instead of the first line being a normal GET or POST like so:
GET /index.html HTTP/1.1
Host: www.example.com
How can this be replaced with something just like:
CUSTOM
Host: [IP]
I don't mind having to use any additional libraries if necessary such as cURL.
UPDATE:
I've tried using the following cURL code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://[IP]:[Port]/");
curl_setopt($ch, CURLOPT_PORT, [Port]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "CUSTOM");
$output = curl_exec($ch);
curl_close($ch);
print($output);
But it just keeps loading for 2 minutes until it said internal error (both with and without using the CURLOPT_CUSTOMREQUEST). However, if I use a standard website such as http://google.com it'll work fine.
Also I forgot to mention, the port my server is using is 7899, is this ok? Open it in my web browser fine, but cURL doesn't seem to be able to.
Looks like there's nothing wrong with your code. If you're using a shared hosting provider, ask them to open up outbound TCP port 7899.
You can create a custom HTTP request method using the http_request_method_register function:
http://www.php.net/manual/en/function.http-request-method-register.php
This code needs to be run on the server that will be handling the request. If you try to use a non-standard HTTP request method on any old server, depending on the server it may ignore it, or may return an error code.

curl_exec($ch) not executing on external domains anymore, why?

I was using cURL to scrape content from a site and just recently my page stated hanging when it reached curl_exec($ch). After some tests I noticed that it could load any other page from my own domain but when attempting to load from anything external I'll get a connect() timeout! error.
Here's a simplified version of what I was using:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://www.google.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
$contents = curl_exec ($ch);
curl_close ($ch);
echo $contents;
?>
Here's some info I have about my host from my phpinfo():
PHP Version 5.3.1
cURL support enabled
cURL Information 7.19.7
Host i686-pc-linux-gnu
I don't have access to SSH or modifying the php.ini file (however I can read it). But is there a way to tell if something was recently set to block cURL access to external domains? Or is there something else I might have missed?
Thanks,
Dave
I'm not aware about any setting like that, it would not make much sense.
As you said you are on a remote webserver without console access I guess that your activity has been detected by the host or more likely it caused issues and so they firewalled you.
A silent iptables DROP would cause this.
When scraping google you need to use proxies for more than a few hand full of requests and you should never abuse your webservers primary IP if it's not your own. That's likely a breach of their TOS and could even result in legal action if they get banned from Google (which can happen).
Take a look at Google rank checker that's a PHP script that does exactly what you want using CURL and proper IP management.
I can't think of anything that's causing a timeout than a firewall on your side.
I'm not sure why you're getting a connect() timeout! error, but the following line:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
If it's not set to 1, it will not return any of the page's content back into your $contents.

Categories