卷曲不发送

时间:2015-04-22 10:22:40

标签: php curl

我的本​​地主机上有这个卷发,这些工作正常。 这些卷发是:[1] - GCM; [2] - 我自己的api。

然而,当将这些文件上传到我的在线服务器时,curl [2]无法正常工作,但curl [1]只是运行良好。

// curl [1] - 我自己的api

public function send($recipient, $message)
{
    // $client = new GuzzleHttp\Client();
//       $r = $client->post('http://link.to.api', [
// 'body' => [
// 'recipient' => $recipient,
//  'message' => $message['message']
// ] ]);
    // $response = $client->send($r);


    // $postData = 'recipient='.$recipient.
                // '&message='.$message['message'];


// shell_exec('curl -i -X POST "http://link.to.api" -d "'. $postData.'"');


    $url = 'http://link.to.api';

    $ch = curl_init();  

    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch,CURLOPT_HEADER, false); 
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    // curl_setopt($ch, CURLOPT_POST, count($postData));

    // Disabling SSL Certificate support temporarly
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  

    curl_setopt($ch, CURLOPT_POSTFIELDS, array('recipient'=>'123123', 'message'=>'assss'));  

    $output=curl_exec($ch);

    curl_close($ch);
    return $output;

}

// curl [2] - GCM

public function send($reg_ids, $message)
{
    // Set POST variables
    $url = 'https://android.googleapis.com/gcm/send';
    $GOOGLE_API_KEY = '11111111111111111111111111111111';


    $fields = array(
        'registration_ids' => $reg_ids,
        'data' => $message,
    );

    $headers = array(
        'Authorization: key=' . $GOOGLE_API_KEY,
        'Content-Type: application/json'
    );
    // 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, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // Disabling SSL Certificate support temporarly
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

    // Execute post
    $result = curl_exec($ch);
    if ($result === FALSE) {
        die('Curl failed: ' . curl_error($ch));
    }

    // Close connection
    curl_close($ch);
    return $result;
}

正如你所看到的,我尝试了不同的选项来发送我的卷曲[2]。 任何提示和帮助?

我做这些事情的网站和我的[link.to.api]存储在同一台服务器上。

1 个答案:

答案 0 :(得分:0)

@csrf_exempt
def test(request):
    # if use form's is_valid() function, it always return false. 
    # very weird, thus i annotate it
    # if request.method == 'POST':
    #     form = UploadFileForm(request.POST, request.FILES)
    #     if form.is_valid():
    user_image = request.FILES['user_image']
    im = UserImage(image=user_image)
    im.save() #after looking for log, it saved many times depending on image size?
    img_path = settings.MEDIA_URL + str(im.image)
    ...
    processing image
    ...

    return render_to_response('open.html', result_data)

我只需将上面的代码更改为:

curl_setopt($ch, CURLOPT_POSTFIELDS, array('recipient'=>'123123', 'message'=>'assss'));

其中$ postData是:

curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

唉!