Reputation:

how to pass the url value in curl php

$description = "some test data and url";
$description .="http://www.mydata.com?test=1&user=4&destination=645&source=stackoverflow";

curl_setopt($sch, CURLOPT_URL, "myserverurl");
curl_setopt($sch, CURLOPT_HEADER, 0);             
curl_setopt($sch, CURLOPT_POST, true);
curl_setopt($sch, CURLOPT_RETURNTRANSFER , 1);
curl_setopt($sch, CURLOPT_POSTFIELDS, "orgid=$orgid&description=$description&external=1");
curl_setopt ($sch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($sch, CURLOPT_SSL_VERIFYPEER, 0); 

when i check on the server (myserverurl).

I can see the description field like

"some test data and url http://www.mydata.com?test=1".

i lost the description after '&'

yes , we can encode the url before sending with curl, but i do not have access to decode the url again on that third party api server

Upvotes: 1

Views: 11280

Answers (4)

Rajesh Hanswal
Rajesh Hanswal

Reputation: 31

This is the easy solution. If you are working on php use function curl_escape()`

$msg=$_POST['Message'];

$ch = curl_init();
$new = curl_escape($ch, $msg);


$ch = curl_init("url".$new."/xyz.com");
$fp = fopen("example_homepage.txt", "w");

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch);
curl_close($ch);
fclose($fp);

Upvotes: 0

Kaleshwar Chand
Kaleshwar Chand

Reputation: 76

Put the data for post in an array and use that array as your CURLOPT_POSTFIELDS. You can get the description in $_POST['description'].

Example
test.php

<?php
$sch = curl_init(); 
$post_data=array();
$orgid='testorg';
$description = "some test data and url";
$description .="http://www.mydata.com?test=1&user=4&destination=645&source=stackoverflow";
$post_data['description']=$description;
$post_data['orgid']=$orgid;
$post_data['external']=1;
curl_setopt($sch, CURLOPT_URL, "localhost/testcurl.php");
curl_setopt($sch, CURLOPT_HEADER, 0);             
curl_setopt($sch, CURLOPT_POST, true);
curl_setopt($sch, CURLOPT_RETURNTRANSFER , 1);
curl_setopt($sch, CURLOPT_POSTFIELDS, $post_data);
$re=curl_exec($sch);

echo('<pre>');
echo($re);

testcurl.php

<?php
var_dump($_POST);

result

array(3) {
  ["description"]=>
  string(94) "some test data and urlhttp://www.mydata.com?test=1&user=4&destination=645&source=stackoverflow"
  ["orgid"]=>
  string(7) "testorg"
  ["external"]=>
  string(1) "1"
}

Upvotes: 0

mrinterested
mrinterested

Reputation: 155

Easy. You can simply get the current URL after it has been entered.

So if you enter

http://www.mydata.com?test=1&user=4&destination=645&source=stackoverflow

(I assume, that mydata.com is your server), you can echo it very easily:

$url = str_replace("?","",$_SERVER['REQUEST_URI']);
echo $url;

And the above echo should give:

test=1&user=4&destination=645&source=stackoverflow

From then, you can simply either save the entire string to the database, or save individual variables (test, user, destination, source) by either $_SERVER['test'], or, if a number of them varies, you can explode them by & character to save them dynamically.

Upvotes: 0

Pascal MARTIN
Pascal MARTIN

Reputation: 400912

What if you urlencode the value of each parameter you are sending ?

You don't have to worry about decoding on the other side : it is standard way of sending data through GET / POST

Something like :

curl_setopt($sch, CURLOPT_POSTFIELDS, 
    "orgid=" . urlencode($orgid) 
    . "&description=" . urlencode($description) 
    . "&external=1"
);

And if this doesn't work, try with rawurlencode ? (there is a difference for spaces, if I remember correctly)

Upvotes: 1

Related Questions