Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a curl response in string format

Tags:

php

curl

I am trying to execute a url and getting its response. Following is the code that executes the curl. I want the curl execution to return me a string in $result.

<?php
$fields = array
(
'username'=>urlencode($username),
'pwrd'=>urlencode($pwrd),
'customer_num'=>urlencode($customer_num)
);

$url = 'http://localhost/test200.php';
//open connection
set_time_limit(20);
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);
echo $result; //I want $result to be "Successful"
?>

This is my test200.php on localhost:

<?php
$usernam = $_POST['username'];
$pass = $_POST['pwrd'];
$customer_num = $_POST['customer_num'];
echo "Successful!";
?>

What changes do I make in test200.php? Please help.

like image 980
user1051505 Avatar asked Jun 17 '26 01:06

user1051505


1 Answers

You should use the httpcode returned by the curl execution and not rely on a string that is returned

$res = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

Here - http://www.webmasterworld.com/forum88/12492.htm

like image 165
Pratik Mandrekar Avatar answered Jun 19 '26 19:06

Pratik Mandrekar