I have been trying to get curl to send a GET method from a form. I have constructed the URL with all the form data concatenated. If I type in the get request directly into the address bar it works but will not work with the curl. If i echo the $data I will actually receive an error stating "Object not found!" I tried displaying curl_error() contents and it return empty. Thank you very much everyone for your help.
This is what i have so far:
$url = complete url of get request that works when directly placed into adress bar
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec ($ch);
curl_close ($ch);
echo $data;
Thank you for helping me everyone. I tried the following code and it does say "sucess". When I echo $data, I see a "sucess" for a split second and then I get the "Object not found!" error.
$url = complete url of get request that works when directly placed into adress bar
$ch = curl_init($url);
if ($ch) {
echo "Success";
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec ($ch);
开发者_StackOverflow curl_close ($ch);
}
else {
echo "Could not initialize curl session";
}
I may be getting errors because the the link I forward the GET to will forward it to another link to process the data.
Try adding the
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1) ;
This should force it to return the data as a string instead of outputting it.
See the documentation for more details.
Try using the CURLOPT_RETURNTRANSFER option:
$ch = curl_init();
$url = complete url of get request that works when directly placed into adress bar
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$data = curl_exec ($ch);
curl_close ($ch);
echo $data;
I believe you are missing a curl_init to initialize the session.
Edit with suggested code
Try copying and pasting the below code:
$url = complete url of get request that works when directly placed into adress bar
$ch = curl_init($url);
if ($ch) {
echo "Success";
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec ($ch);
curl_close ($ch);
echo $data;
}
else {
echo "Could not initialize curl session";
}
And let us know if the curl session is initialized successfully.
I faced the same issue and resolved it as follows:
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS , null );
curl_setopt($ch, CURLOPT_POST , 0);
curl_setopt($ch, CURLOPT_HTTPGET , TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER , TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT , 4);
curl_setopt($ch, CURLOPT_USERAGENT , "" );
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_URL , $url );
$rgxrData = curl_exec($ch); // rgxrData stands for response get xml raw Data
echo "Get curl_errno = " ;
echo curl_errno($ch) ;
if (curl_errno($ch)) {
$error_message = curl_error($ch);
$error_no = curl_errno($ch);
echo "error_message: " . $error_message . "<br>";
echo "error_no: " . $error_no . "<br>";
}
curl_close($ch);
return $rgxrData;
It is important that you follow the correct order for reset from post to get. ie first set the postfields to null, then post to 0 and then httpget to true. once you have set this then you can be assured that the server will start preparing the next request like a get request else not. Thereafter, set the rest of the option and the url(along with all the parameters). This will ensure the get request is properly made and executed.
Hope this helps...
Please try http_build_query() function for create query string i think this will help , it is work in my case
精彩评论