PHP Curl

时间:2016-08-30 21:52:32

标签: php json curl

我正在尝试创建一个让Apple用户更轻松的脚本,我现在不想真正公开它,但这些是我需要遵循的步骤。

  1. 登录Apple并保存Cookie。
  2. 使用已保存的Cookie从Apple获取数据。
  3. 创建自动化工具。
  4. 我陷入了第一步,Curl没有提交自定义标题,因为我需要提交json标题。

    这是我的代码:

    <?php
    function login($url,$data){
        $data=json_encode($data);
        $login = curl_init();
        curl_setopt($login, CURLOPT_COOKIEJAR, "cookie.txt");
        curl_setopt($login, CURLOPT_COOKIEFILE, "cookie.txt");
        curl_setopt($login, CURLOPT_NOBODY, false);
        curl_setopt($login, CURLOPT_HEADER, false);
        curl_setopt($login, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($login, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($login, CURLOPT_TIMEOUT, 40000);
        curl_setopt($login, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($login, CURLOPT_URL, $url);
        curl_setopt($login, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        curl_setopt($login, CURLOPT_FOLLOWLOCATION, TRUE);
        curl_setopt($login, CURLOPT_POST, TRUE);
        curl_setopt($login, CURLOPT_POST, 1); 
        curl_setopt($login, CURLOPT_HTTPHEADER, array(                                                                          
        'Content-Type: application/json',                                                                                
        'Content-Length: ' . strlen($data))                                                                       
    );     
        curl_setopt($login, CURLOPT_POSTFIELDS, $data);
        //ob_start();
        $rez=curl_exec ($login);  
    }                   
    
    $data = array("accountName" => "myemail@me.com", "password" => "MyPassWord","rememberMe" => "true"); 
    $url="https://idmsa.apple.com/appleauth/auth/signin";    
    login($url,$data);
    ?>
    

    每当我在发送请求时尝试篡改数据时,它都不会显示自定义标题

2 个答案:

答案 0 :(得分:0)

作为一个评论,这将是无法管理的,所以这里是我see的内容:

        $verbiage = null;
        if ($this->verbose) {
            curl_setopt($ch , CURLINFO_HEADER_OUT , false);
            curl_setopt($ch , CURLOPT_VERBOSE , true);
            $verbiage = fopen('php://temp' , 'w+');
            curl_setopt($ch , CURLOPT_STDERR , $verbiage);
        }

        $resp = curl_exec($ch);

        if ($this->verbose) {
            rewind($verbiage);
            $verboseLog = stream_get_contents($verbiage);
            $log->info("Verbose information:\n$verboseLog");
            fclose($verbiage);
        }


        curl_close($ch);

其中$ log是一个log4php对象,你选择你的显示方法。

编辑使用此跟踪添加示例日志&#39;选项已启用

INFO - testCreateLabRequest starts.
TRACE - Query for lab request [dispensary[1]inboundLabRequest[2]ForPatient[1]] returned no records for feed [toubibCreamedFeedGuid]
DEBUG - Sending json 
{ *redacted* , "trackingId":"5ac4473b-407f-4d7d-83eb-1a3f9abe5b8b"}

INFO - Verbose information:
*   Trying fe80::3636:3bff:fecf:804c...
* Connected to darkmax.local (fe80::3636:3bff:fecf:804c) port 80 (#0)
> POST /ehr/feed/lab/request HTTP/1.1
Host: darkmax.local
Protocol_version : 1.1
Connection: Close
Content-Type: application/json ; charset=utf-8
Proxy-Connection: Close
User-Agent: Portable EHR(106)
Accept: application/json, text/html;
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Content-Length: 851

* upload completely sent off: 851 out of 851 bytes
< HTTP/1.1 200 OK
< Date: Wed, 31 Aug 2016 02:04:03 GMT
< Server: Apachee/2.4.12
< Access-Control-Allow-Origin: *
< Origin: http://darkmax.local
< Connection: close
< Transfer-Encoding: chunked
< Content-Type: application/json;  charset=utf-8
< 
* Closing connection 0

<行与我的cURL会话从远程对等方接收的行相对应。对我从对手回到我身边的实际HTTP协议有一些了解已经帮助我多次弄清楚某些潜在问题的本质。

答案 1 :(得分:0)

Apple使用X-Apple-Widget-Key之类的CSRF。我想你需要抓住它。您可以在source code找到它。您可以使用curl_getinfo()

获取状态

enter image description here     //你的代码......

curl_setopt($login, CURLOPT_POSTFIELDS, $data);
//ob_start();
$rez = curl_exec($login);
$status = curl_getinfo($login);

print_r($status);