Http_request with curl

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
highjo
Forum Contributor
Posts: 118
Joined: Tue Oct 24, 2006 1:07 pm

Http_request with curl

Post by highjo »

hi! there i'm trying to build an small system that i can call from anywhere on the web to do a certain action for me.I'm using curl library for that.For testing my logique i created 2 files test.php and test1.php in the same folder,the first as a client and the second which will process some action and return a value that the first file can use.What i see is so weird that i don't even know that i understand
here are they
this is test.php

Code: Select all

 
$ch = curl_init("./test1.php");
 
curl_setopt($ch, CURLOPT_URL,"test2.php");
$param = "jojo";
curl_setopt($ch,CURLOPT_POSTFIELDS,$param);
$i =curl_exec($ch);
curl_close($ch);
echo $i;
 
this is test1.php

Code: Select all

 
if(isset($_REQUEST['name']))
{
    $name = $_REQUEST['name'];
    
    /*$d = new mysqli("localhost","root","mamamia","ajax");
    
    $res=$d->query("INSERT INTO users `user_name` VALUES ('$name')");
    echo $res->affected_rows();
    $d->close();*/
    return $name;
}
 
and this is what the server display
$a[one] => 1. $a[two] => 2. $a[three] => 3. $a[seventeen] => 17.
:crazy:
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Re: Http_request with curl

Post by kendall »

try looking up the php description for the below function
curl_connect()
nowaydown1
Forum Contributor
Posts: 169
Joined: Sun Apr 27, 2008 1:22 am

Re: Http_request with curl

Post by nowaydown1 »

Not sure what the data is that you're seeing, but two things that I see just looking at your CURL script. I assume that you're trying to output the contents of the curl request with the echo $i (unless it was really your intent to echo the boolean result value of your curl_exec). You need an additional curl_setopt to do that:

Code: Select all

 
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
 
That will return the contents of your curl request. Additionally, as far as I know, your CURLOPT_POSTFIELDS should appear as the typical query string key, value pair format:

Code: Select all

 
curl_setopt($ch, CURLOPT_POSTFIELDS, "string=This is a string"); 
 
Not sure if any of that is related to your issue, but just what I noticed. Hope that helps. :)
User avatar
highjo
Forum Contributor
Posts: 118
Joined: Tue Oct 24, 2006 1:07 pm

Re: Http_request with curl

Post by highjo »

hey man it does help.It opened my mind on that something i'm missing.thanks man.i'll try that
sim-and-sim
Forum Newbie
Posts: 6
Joined: Sun May 18, 2008 9:54 am

Re: Http_request with curl

Post by sim-and-sim »

I think I understand what you're trying to do

Code: Select all

 
<?php 
//Start a new cURL
$ch = curl_init();
 
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "test1.php"); //Put the file you want to be posted to here I think yours is test1.php, its not the file this code is in, its the file that you want to return
 
// Do a POST
$data = array("name" => "string", "name2" => "string2",); //This here is your post Array, what you want to be posted
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //Just encase there is a header("Location: file.php"); in the file
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //If you don't include this, you won't get the file back as a string, it will just be printed
curl_setopt($ch, CURLOPT_POST, true); //This says that you want stuff to be posted
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //This is the fields you want to be posted
 
// grab URL, and print
$output = curl_exec($ch); //This is what it returns;
echo $output;
 
?>
 
hope that helps
Post Reply