0

So I am building a script in PHP which submit datas to a third-party site using a HTTP POST request via Curl. I have been successful in getting this to work with 4 pages so far, however there is one which doesn't want to play.

I have used HTTPTrack in Chome to track exactly what POST data is submitted when the action is performed in the browser. I have also successfully manually posted this data back using HTTPRequester in Firefox and the site return "200 OK" and accepts the request and processes the data.

However when the exact same request is made in Curl, it returns a "200 OK", but doesn't process the data. I have tracked the HTTP POST in tcpdump on the server and it is identical to that submitted by Chome and manually in Firefox. Also the method used to submit the data is the same in the other 4 scripts which do work so I don't believe this is something code related as such.

But I am missing something and I am not sure what, any ideas?

Code as requested...

// Send post data
$url = "http://www.thesite.com/user.aspx";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/32.0.1700.107 Chrome/32.0.1700.107 Safari/537.36');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postinfo);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, '/home/site/cookies/c.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, '/home/site/cookies/c.txt');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
//curl_setopt($ch, CURLOPT_HEADER, true);
//curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$html = curl_exec($ch);
  • 1
    It would be helpfull if we could see the essential parts of your code. – davejal Jan 03 '16 at 20:21
  • Not only you have to make sure they have identical POST, probably some identical headers too. Cookies, session, referer, custom header, other hidden field, redirection, code related... – frz3993 Jan 03 '16 at 20:25
  • 1
    @frz3993 tcpdump data should contain all of that – Frederick Zhang Jan 03 '16 at 20:27
  • @frz3993 The exact same code is working for the other 4 pages, therefore cookies, session, referrer, headers, etc.... are already working. The only thing which changes between the pages is the data being submitted. The data being submitted, matches that in Chome and Firefox which I have proven to work manually. –  Jan 03 '16 at 20:31
  • If you're really sure about the tcpdump data and have checked all factors such as UA, Session, Header and etc., maybe your server is just blocked by the requested server? You can set up a proxy on your server and test it in your Chrome. – Frederick Zhang Jan 03 '16 at 20:33
  • @FrederickZhang squid proxy server option already explored and result is the same. –  Jan 03 '16 at 20:35
  • I have a PHP script which has the same function so I compared mine with yours. The only difference is that mine has a `Content-Type` header but yours not. I'm not sure if this is the reason but it's better to include the header if you're posting forms or JSONs. – Frederick Zhang Jan 03 '16 at 20:43
  • @FrederickZhang I'll give it a go now ... –  Jan 03 '16 at 20:49
  • may be this help you http://stackoverflow.com/a/32583740/1196580 – bmavus Jan 03 '16 at 21:07
  • You are using Cookies - is there a server side state/session which is different between the browser and curl based requests? – Steve E. Jan 03 '16 at 21:48
  • @FrederickZhang, Content-Type is not the answer :/ –  Jan 03 '16 at 22:03
  • @bmavus How does that help? –  Jan 03 '16 at 22:04
  • @SteveE. In a word, no –  Jan 03 '16 at 22:05

0 Answers0