Curl get url with curl_getinfo - php

Hi I know its a very common topic on StackOverFlow. I have already spent my entire week to search it out.
Such as I have a url : http://bayja.com/forum/index.php
This url I want to get after I post : http://bayja.com/forum/index.php?topic=3454.0
but now I get http://bayja.com/forum/index.php?board=2.0
I use $lastUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
How can I get "http://bayja.com/forum/index.php?topic=3454.0
" ?
Please help me thank you.
here is my code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__).'/cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__).'/cookie.txt');
curl_setopt($ch, CURLOPT_USERAGENT, "Automatic SMF poster thing");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "user=".$user."&passwrd=".$password);
curl_setopt($ch, CURLOPT_URL, "$domain?action=login2");
curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, "$domain?action=post;board=".$board.".0");
$data = curl_exec($ch);
sleep(3);
preg_match('/name="([^"]+)" value="([^"]{32})"/',$data,$sc);
preg_match ( "<input type=\"hidden\" name=\"seqnum\" value=\"(.*)\">", $data, $seqnum);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "topic=".$topic."&subject=".urlencode($subj)."&icon=xx&sel_face=&sel_size=&sel_color=&message=".urlencode($mess)."&message_mode=0&notify=0&lock=0&post_vv[q][4]=๔&post_vv[q][3]=๒&post_vv[q][1]=๔&post_vv[q][2]=๒&additional_options=0&post=xxxxx&".$sc[1]."=".$sc[2]."&seqnum=4&seqnum=".$seqnum[1]);
curl_setopt($ch, CURLOPT_URL, "$domain?action=post2;start=0;board=".$board);
curl_exec($ch);
$lastUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
echo $lastUrl; // output "http://bayja.com/forum/index.php?board2"
curl_close($ch);

Related

POST data and redirect user by PHP CURL

We need to redirect users to a URL and send some data to that URL (POST) with PHP CURL. Exactly like when a user click on HTML form submit with POST method.
Our code is
$data=array("Token"=>"test2","RedirectURL"=>"test1");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://sep.shaparak.ir/Payment.aspx');
curl_setopt($ch, CURLOPT_REFERER, 'https://sep.shaparak.ir/Payment.aspx');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_POSTREDIR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_exec($ch);
But it doesn't work.
What is the solution?
As I found, it is not possible just with PHP.
You can generate a form with PHP and submit that with JS in onLoad.
Here is more detail.
This is my workable solution for post with curl:
$postdata = "email=".$username."&pass=".$password;
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, sizeof($postdata));
curl_setopt($ch,CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec ($ch);
curl_close ($ch);
There is an alternative solution if above doesnt works:
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec ($ch);
curl_close ($ch);

Curling HTTPS does not resolve

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $curl_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_CAINFO, dirname(__FILE__)."/cacert.pem");
// Edit: prior variable $postFields should be $postfields;
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
if(curl_errno($ch))
{
echo 'error:' . curl_error($ch);
}
print_r($result);
$array_json = json_decode($result, true);
I tried to do a curl to a https site, it say the dns cannot be resolve. But that site I can access it by browser
https://example.com [example]
I did test on my own local installment
$ch = curl_init();
$curl_url = 'https://example.com';
curl_setopt($ch, CURLOPT_URL, $curl_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, 1);
//curl_setopt ($ch, CURLOPT_CAINFO, dirname(__FILE__)."/cacert.pem");
// Edit: prior variable $postFields should be $postfields;
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
if(curl_errno($ch))
{
echo 'error:' . curl_error($ch);
}
print_r($result);
It works correct and print result. I suppose you problem is in this string
curl_setopt ($ch, CURLOPT_CAINFO, dirname(__FILE__)."/cacert.pem");
You could past wrong path or load wrong certificate. I comment this line in my example.

Get post result with cURL and PHP

I have a little function which performs a form submission and it should return the result (html).
The form has only one text input:
<input type="text" value="" maxlength="5" name="CODE" size="10">
This is how I try to fill this variable with cURL:
//open connection
$ch = curl_init();
$myValue = "Test123";
$formUrl = "http://www.mywebsite.com/post.php";
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $formUrl);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "CODE=".$myValue);
//execute post
$result = curl_exec($ch);
$info = curl_getinfo($ch);
//close connection
curl_close($ch);
echo $result;
My problem is that I don't get any results... Is there any problem with this code?
Thank you very much for the help!
EDIT:
I also tried with this kind of setup, with the same result:
$formData = array('CHKCODE' => $tesseractOutput);
curl_setopt($ch, CURLOPT_URL, $formUrl);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($formData));
You could try a bit different approach. A simple example:
$data = array("CODE" => "Test123");
//$ch = curl_init("http://www.mywebsite.com/post.php");
//EDIT
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
//EDIT
curl_setopt($ch, CURLOPT_URL, "http://www.mywebsite.com/post.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
$response = curl_exec($ch);
var_dump($response);
curl_close($ch);

cURL XML Post issue

Im posting some XML to a webservice, the code I have written works in all other cases just not in this instance.
This is my code;
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_setopt($ch, CURLOPT_ENCODING,'gzip');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 45);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
$result = curl_exec($ch);
curl_close($ch);
return $result;
It keeps returning a 500 internal server error which is the same as if you go direct to the url so it's like the POST data isn't being sent. But I haven't check the content length and it seems to be..
Any Ideas?
try just this
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_string); // NOTE used $xml_string to indicate its just a text string of XML format
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_NOPROGRESS, 0);
$result = curl_exec($ch);
curl_close($ch);

How to send a Delete request from PHP

The best exemple i have found is:
$request_body = 'some data';
$ch = curl_init('http://www.example.com');
curl_setopt($ch, CURLOPT_POSTFIELDS, $request_body);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
$response = curl_exec($ch);
var_dump($response);
Though this is to deltete an FB app request so the URL should look like this: https://graph.facebook.com/[request_id]?access_token=USER_ACCESS_TOKEN
Could someone show me how to implement that code for my case ?
Do not use curl_setopt($ch, CURLOPT_POSTFIELDS, $request_body);
Use
$ch = curl_init('https://graph.facebook.com/'.$request_uri.'&access_token='.$access_token);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
$response = curl_exec($ch);
var_dump($response);

Categories