dsd57259 2014-10-17 12:57
浏览 107

使用PHP将CURL POST到JSON(使用Cookie.txt)

I want to POST some CURL to a REST API with the use of a PHP Frontend. A Short Description of my Project ... The user (we call him Anton) logs in, on a classic Login Form on the Startsite. After that the user should be logged in until he wants to logout (But thats not the issue here). The main task of the user is to fill a Form with a projectname and a description. After a submit, the data are transferred to a JSON Object which creates the Project in the Software. When I run the CURL commands manually via the terminal, everything works fine. But my PHP Script, unfortunately not.

Login works fine in Terminal

curl -v \
     -c cookie.txt \
     -X POST \
     -F j_username=Anton \
     -F j_password=pw \
     http://test.com/login.json

Login works fine in PHP

<?php
        //Login
        error_reporting(E_ALL|E_STRICT);
        ini_set('display_errors', '1');

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "http://test.com/login.json");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt ($ch, CURLOPT_COOKIEJAR, dirname(__FILE__).'/cookie.txt');
        curl_setopt ($ch, CURLOPT_COOKIEFILE, dirname(__FILE__).'/cookie.txt');  
        curl_setopt($ch, CURLOPT_POST, true);

        $data = array(
            'j_username' => 'Anton',
            'j_password' => 'pw',
        );

        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $output = curl_exec($ch);
        $info = curl_getinfo($ch);
        curl_close($ch);
        ?>

**create Projekt works fine in Terminal but not in php Create a Projekt ... i have absolutely no idea ! How do i use the Cookie.txt and how do i fill the array (-d part) with the data ?

... and how can I stay logged in to create a projekt?

curl -v \
         -b cookie.txt \
         -X POST \
         -H "Content-Type: application/json" \
         -d '{"name":"name12345", "description":"Some Kind of Description"}' \
         http://test.com/productions.json

edit... but it actually not work. i have problems to use the cookie.txt :(

<?php
// //Create Production

function post_to_url($url, $data) {
   $fields = '';
   foreach($data as $key => $value) { 
      $fields .= $key . '=' . $value . '&'; 
   }
   rtrim($fields, '&');

   $post = curl_init();

   curl_setopt($post, CURLOPT_URL, $url);
   curl_setopt($post, CURLOPT_COOKIEFILE, dirname(__FILE__).'/cookie.txt');
   curl_setopt($post, CURLOPT_COOKIEJAR, dirname(__FILE__).'/cookie.txt');
   curl_setopt($post, CURLOPT_POST, count($data));
   curl_setopt($post, CURLOPT_POSTFIELDS, $fields);
   curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);

   $result = curl_exec($post);

   curl_close($post);
}

$data = array(
   "name" => "Max",
   "description" => "Some kind of description"
);


post_to_url("http://test.com/productions.json", $data);

print_r($data);

?>

I strongly believe in your help. Thanks!

  • 写回答

1条回答 默认 最新

  • doujiene2845 2014-10-20 07:55
    关注

    it would be very very very kind of you, if someone could translate THIS Curl POST...

    curl -v \
             -b cookie.txt \
             -X POST \
             -H "Content-Type: application/json" \
             -d '{"name":"name12345", "description":"Some Kind of Description"}' \
             http://test.com/productions.json
    

    ... to PHP with the use of CURL_SETOPT.

    Thank you very much !!!!

    评论

报告相同问题?

悬赏问题

  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。