cURL在Facebook API SDK上抛出异常

时间:2013-01-15 17:01:16

标签: facebook apache curl wamp

我正在尝试从我的Facebook应用程序发布图像作为用户。 目前,此功能仍处于开发阶段,当我尝试使用WAMP server上的localhost进行测试时,cURL类中的Facebook API SDK函数之一,抛出异常错误:

  

{“error_code”:60,“error”:{“message”:“SSL证书问题,请验证   CA证书没问题。详细信息:\ n错误:14090086:SSL   例程:SSL3_GET_SERVER_CERTIFICATE:证书验证   失败”, “类型”: “CurlException”}} null0.90752005577087   145684992172054 | e259ec293812a5276a61d3181c395077

我已检查过,php.iniApache目录PHP处的设置正确。

我尝试在Easy PHP上使用PHP 5.4WAMP上使用PHP 5.3运行此功能。 (启用了cURL扩展)。

我在两个本地服务器上安装了open SSL

我知道您可以通过更改CURL_opt的配置来解决这个问题,但修改Facebook基类,并破坏我的应用程序的安全性(中间人攻击),对我来说真的是最后的手段。

提前感谢您的帮助!

我的代码:

test.php:
<?php
session_start();

require 'FB_auth.php';
require 'includes.php';
require 'functions_fb.php';

    $start = (float) array_sum(explode(' ',microtime())); 

    //echo json_encode(photoToFacebook(299, 402, 134));
    // echo json_encode(photoToFacebook(724, 402, 165));

    // postToFacebook($db, 402, 134);

    $end = (float) array_sum(explode(' ',microtime())); 
    $duration = ($end-$start);

    echo $duration;
    echo "<br/>" . $facebook->getAccessToken();

?>

functions_fb.php

<?php

function photoToFacebook($hr_id, $user_id, $job_id){


        global $db, $FEED_PATH, $JOB_IMAGES_FOLDER , $facebook;
        $path = $_SERVER['DOCUMENT_ROOT'] . "/";
        $JOB_IMAGES_FOLDER = "images/job_images/";



        $company = $db->getCompany($hr_id);

        // perapre the job photo.
        $photo = $db->getJobPhoto($job_id);
        if (empty($photo)){
            $photo = $db->getLogo($hr_id);
        }
        else{
            $photo = $JOB_IMAGES_FOLDER . $photo;
        }

        try{
            // At the time of writing it is necessary to enable upload support in the Facebook SDK, you do this with the line:
            $facebook->setFileUploadSupport(true);

            // Get album id if exists.
            $album_uid = $db->getAlbumId($user_id);
            $fb_user_id = $db->getFBUserId($user_id);
            if (empty($album_uid)){

                // Create an album
                $album_details = array(
                        'message'=> 'Check out my company\'s job opportunities',
                        'name'=> "Job opportunities at {$company}"
                );


                $create_album = $facebook->api("/{$fb_user_id}/albums", 'post', $album_details);
                echo json_encode($create_album);

                $album_uid = $create_album["id"];
                $db->saveAlbumId($user_id, $create_album["id"]);
            }
            $job_title = $db->getJobTitle($job_id);

            $link = Bitly::make_bitly_url($FEED_PATH . "show_job.php?jid={$job_id}&th_uid={$user_id}&tp=1&pc=1", BITLY_APP_USERNAME,BITLY_APP_KEY,'json');
            $photo_details = array(
                'message'=> $job_title . "!\n" . $company . " is looking for you.\nOpen the link for details and apply.\n" . $link
            );

            $path .= $photo;
            $photo_details['image'] = '@' . $path; //realpath($file);

            // echo $album_uid;

            $upload_photo = $facebook->api("{$fb_user_id}/photos", 'post', $photo_details);

            // $db->updateAutoPostActivity($user_id, $job_id,1, json_encode($upload_photo));
            // $db->updateAutoPostJob($job_id, $user_id);

            return $upload_photo;
        }
        catch (FacebookApiException $e) {
            echo "error:" . json_encode($e->getResult());
            // $db->updateAutoPostActivity($user_id, $job_id,1, json_encode($e->getResult()));
            // AtavMail::SendRichedMail(null , "", "Error on auto process - posting photo to FB", "Where - Job Id: {$job_id} , User Id: {$user_id}", json_encode($e->getResult()));
        }

    }
?>

2 个答案:

答案 0 :(得分:1)

我通过在base_facebook.php文件中添加以下行来解决这个问题:

  public static $CURL_OPTS = array(
     //
     //
     //
    CURLOPT_SSL_VERIFYPEER => false,
  );

请注意,这只是一个开发环境解决方案,在实际应用中使用此解决方案可能会带来安全风险。

请花点时间在此链接上查看完整的正确解决方案: http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/

答案 1 :(得分:1)

将SSL验证设置为false会破坏SSL的目的。 一个合适的解决方案是为CURL设置CA证书。

解决方案1(对PHP.ini的更改):

  1. http://curl.haxx.se/docs/caextract.html
  2. 下载CA捆绑包(cacert.pem)
  3. 将其放在您的本地系统上(例如:C:\ xampp \ cacert.pem)
  4. 打开你的php.ini
  5. 将curl.ca_info选项设置为指向cacert.pem的位置

    Example: curl.ca_info="C:\xampp\cacert.pem"
    
  6. 重启Apache

  7. 解决方案2(在每次CURL调用之前设置选项)

    1. http://curl.haxx.se/docs/caextract.html
    2. 下载CA捆绑包(cacert.pem)
    3. 将其放在您的本地系统上(例如:C:\ xampp \ cacert.pem)
    4. 编写以下代码:

      curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, TRUE); 
      curl_setopt ($ch, CURLOPT_CAINFO, "pathto\cacert.pem");
      
    5. 来源:http://tumblr.wehavefaces.net/post/52114563111/environment-windows-xampp-curl-library