Jump to content

php/curl headaches


MajusC00L

Recommended Posts

Hello Freaks

 

I've spent the lats two weeks battling with PHP scripts where I try to post keywords to XML engine and get some results back on my site. I went quite far but I encounter problems for which I apparently can't find any solutions. So yesterday, I thought that I probably lack some basics and I recreated the script in a simpler version to try to identify where I'm doing something wrong. It was a good idea because even with this very basic approach of fetching a page with PHP/CURL, there are things that do not work. Hence this post in a a desperate hope to get back on the right tracks.

 

 

1- The idea is the following: A simple form (with a POST method) ask for a keyword wich is used by the remaiuning code to post a PHP/CURL request to an image database RSS search engine. You can see this script in action at the following address: http://www.fotonumerix.com/misc/form01_002.php

 

The code for this page is:

<html>
<head>
<title> PHP/CURL Search form
</head>
<body>

<form action="form01_002.php" method="post">
  Keywords: <input type="test" name="keywords">
  <input type="submit">
</form>


<?php
$result = "";
$url_prefix = "http://www.crestock.com/rss/keyword.aspx?"; 
$url_suffix = "keyword=".$_REQUEST["keywords"];
$url = $url_prefix.$url_suffix;

$ch = curl_init();
[color=green][b]	curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);[/b][/color]
$result=curl_exec($ch);
curl_close($ch);

echo $result;
?>
</body>
</html>

 

This page works almost correctly. But I don't understand why I'm sometimes presented the form to fill and sometimes the results are displayed directly and there is no way I can have the form displayed (first mystery)

 

The other page is a more complex version of this first script and it shows exactly where my problem of the last two weeks lies. It's almost the same script exept that it uses the CURLOPT_POST and CURLOPT_POSTFIELD options.  As soon as I use these options, there is no way I can have get results. PHP/CURL usage seems to be so simple to use. I really can't see what I'm doing wrong. This second script is at http://www.fotonumerix.com/misc/form01_003.php and the code is:

<html>
<head>
<title> PHP/CURL Search form
</head>
<body>

<form action="form01_003.php" method="post">
  Keywords: <input type="test" name="keywords">
  <input type="submit">
</form>

<?php
$result = "";
$url_prefix = "http://www.crestock.com/rss/keyword.aspx?"; 
$url_suffix = "keyword=".$_REQUEST["keywords"]; //create a string type like "[i]keyword=anykeywords[/i]" appended to $url_prefix

// $url = $url_prefix.$url_suffix; // not used in this version

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_prefix);
[color=red][b]	curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $url_suffix);[/b][/color]
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result=curl_exec($ch);
curl_close($ch);

echo $result;
?>
</body>
</html>

 

 

"How the heck can I make this second script work?" would be my main question on this post

 

Thanks in advance for any help provided. I'm totally blinded on this one.

 

Friendly

 

MajusC00L

 

 

Link to comment
https://forums.phpfreaks.com/topic/103698-phpcurl-headaches/
Share on other sites

 

Hi Priti and thanks for the link, but I already had it in my favorites and copied the whole code used it as an inspiration to create my more complex script, the one I'm having problem with for two weeks. That is why yesterday I created these two simpler scripts to try to target the problem more precisely. When I will finally find out what I do not understand today and make these two scripts work, the one I created with the askapache tutorial will also work I reckon.

 

There must be something really stupid I don't see in these two scripts. Fetching a simple URL with predetermine url is easy with cURL but quite pointless. Form submitted URL shoul not be more difficult but the fact is that something does not work and finding what the problem is would be a great relief ;-)

 

 

Link to comment
https://forums.phpfreaks.com/topic/103698-phpcurl-headaches/#findComment-531043
Share on other sites

...This page works almost correctly. But I don't understand why I'm sometimes presented the form to fill and sometimes the results are displayed directly and there is no way I can have the form displayed (first mystery)...

 

This first mystery has been solved with the help of less sleepy chap by closing the <title>  tag. I knew I was probably blinded on this one but not by something so obvious  ::)

 

So my second problem remains though (imagine the <title> tag correctly closed). As soon as I start using the CURLOPT_POST and CURLOPT_POSTFIELDS options, the curl_exec always return empty and nothing is echoed. Anyone has a clue?

Link to comment
https://forums.phpfreaks.com/topic/103698-phpcurl-headaches/#findComment-531342
Share on other sites

So my second problem remains though (imagine the <title> tag correctly closed). As soon as I start using the CURLOPT_POST and CURLOPT_POSTFIELDS options, the curl_exec always return empty and nothing is echoed. Anyone has a clue?

 

I had an idea last night but I don't know if it's a good track to follow:

 

Can my problem have something to do with the use of .htaccess file. I am not using any yet. I saw on some examples found on the web that the following code was used for php/curl and POST:

 

RewriteEngine On
RewriteBase /
RewriteRule ^IdunnoWhatToPutHere$ /myscript.php

 

I do not understand why url rewriting would be necessary if my search url is correctly formed by my script but I set up a .htaccess file like this anyway but got no results. Can my problem be there?

Link to comment
https://forums.phpfreaks.com/topic/103698-phpcurl-headaches/#findComment-531579
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.