0

I am new to using curl. I would like you to help me creating a code for this url in curl.

http://example.com/example/example.aspx?post1=xxx&post2=xxx&post3=xxx&post4=xxx&post5=xxxxxxxx&post6=xx&post7=xxxxxxx

I want to create a php file which has to get the similar data and post the data to above url using curl. Thanks for your help once again.

<?php

$url = "http://example.com/example/example.aspx";

$fields = array(
    'post1' => 'xxx',
    'post2' => 'xxx',
    'post3' => 'xxx',
    'post4' => 'xxx',
    'post5' => 'xxx',
    'post6' => 'xxx',
    'post7' => 'xxx'
);
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

//open connection
$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);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

?>
Vignesh
  • 13
  • 3

1 Answers1

1

Try

    $data = 'post1=xxx&post2=xxx&post3=xxx&post4=xxx&post5=xxxxxxxx&post6=xx&post7=xxxxxxx';
    $ch = curl_init('http://example.com/example/example.aspx');
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $result = curl_exec($ch);
    curl_close($ch);

For more :- Passing $_POST values with cURL

and manual which you need to read :- http://php.net/manual/en/book.curl.php

Community
  • 1
  • 1
Rakesh Sharma
  • 13,680
  • 5
  • 37
  • 44